#include <bits/stdc++.h>
#include<ctime>
using namespace std;

#define sp ' '
#define ar array
#define fi first
#define se second
#define vt vector
#define ends "\n"
#define pb push_back
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

const long long MOD=1e9+7;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

void DBG() {
	cerr << "]" << endl;
}
template<class H, class... T> void DBG(H h, T... t) {
	cerr << to_string(h);
	if(sizeof...(t))
		cerr << ", ";
	DBG(t...);
}
#define dbg(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)

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

    int N; cin >> N;
    vector<vector<pair<int, char>>> adj(N);
    vector<bool> vis1(N), vis2(N);
    ll ans=0;
    for(int i=0; i<N-1; ++i){
        int X, Y; char c; cin >> X >> Y >> c, --X, --Y;
        adj[X].pb({Y, c}); adj[Y].pb({X, c});
    }

    function<void(int, int, int, int)>dfs=[&](int v, int cnt1, int cnt2, int cnt3){
        vis1[v]=true;
        if(!vis2[v])
            if(cnt1==cnt2&&cnt1==cnt3)++ans;
        for(auto u:adj[v]){
            if(!vis1[u.fi]){
                int k1=cnt1+(u.se=='S'), k2=cnt2+(u.se=='I'), k3=cnt3+(u.se=='O');
                dfs(u.fi, k1, k2, k3);
            }
        }
    };

    for(int i=0; i<N; ++i){
        for(int j=0; j<N; ++j)vis1[j]=false;
        vis2[i]=true;
        dfs(i, 0, 0, 0);
    }

    cout << ans << ends;
}