#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define pb push_back
#define fi first
#define se second
#define ld long double
#define MAXC 10000000
#define N_MAX 100010

using namespace std;

void solve() {
	int n ;
	cin >> n ;
	vector<char>v(n-1);
	for (int i = 1; i < n ; i++ ) {
	    int a,b;
	    char c ;
	    cin >> a>> b >> c ;
	    v[min(a-1,b-1)] = c ;
	}
	map<pair<int,int>,int>m;
	int a = 0 , b = 0 , res = 0;
	m[{0,0}]++;
	for (auto c:v) {
	    if (c == 'S') {
	        a--;
	        b--;
	        res+=m[{a,b}];
	        m[{a,b}]++;
	    }
	    else if (c == 'I' ) {
	        a++;
	        res+=m[{a,b}];
	        m[{a,b}]++;
	    }
	    else {
	        b++;
	        res+=m[{a,b}];
	        m[{a,b}]++;
	    }
	}
	
	cout << res<< endl ;
} 


int main () {
 	
 	
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

 
    int t =1;
    //cin >>t ;
    while(t--) 
        solve();
}