#include <bits/stdc++.h>
using namespace std;
int n;
int f(vector<char> str)
{
    map< pair<int, int>, int > mp;
    mp[make_pair(0, 0)] = 1;
    int zc = 0, oc = 0, tc = 0;
    long long res = 0;
    for (int i = 0; i < n; ++i)
    {
        if (str[i] == 'O') zc++;
        else if (str[i] == 'I') oc++;
        else tc++;
        pair<int, int> tmp = make_pair(zc - oc, zc - tc);
        res = res + mp[tmp];
        mp[tmp]++;
    }
    return res;
}

/*int main()
{
    string str = "OIOSOIO";
    n=7;
    cout << f(str) << endl;
    return 0;
} */
int main()
{
    cin>>n;
    n--;
    int a, b;
    char c;
    vector<char> s(n);
    for(int i=0; i<n; i++){
        cin>>a>>b>>c;
        s[min(a, b)-1]=c;
    }
    //for(auto c : s) cout<<c;
    cout<<f(s);
    return 0;
}