#include <bits/stdc++.h>
using namespace std;
#define pb push_back
const int N=300050;
vector<pair<int,char>> E[N];
int n,m,x[N],y[N];
char s[N],c[N];
void SubTask1(){
    //deque<pair<pair<int,int>,char>> q;
    pair<pair<int,int>,int> res[n];
    for(int i=0;i<n;i++)res[i].first.first=0,res[i].first.second=0,res[i].second=0;
    pair<pair<int,int>,char> st;
    for(int i=0;i<n-1;i++){
        if(c[i]=='S')st={{x[i],y[i]},c[i]};
    }
    int ans=0;
    for(auto e:E[st.first.first]){
        for(auto r:E[e.first]){
            if(r.first==st.first.first)continue;
            if((e.second=='I'&&r.second=='O')||(e.second=='O'&&r.second=='I'))ans++;
        }
    }
    for(auto e:E[st.first.second]){
        for(auto r:E[e.first]){
            if(r.first==st.first.second)continue;
            if((e.second=='I'&&r.second=='O')||(e.second=='O'&&r.second=='I'))ans++;
        }
    }
    printf("%i",ans);
}
void SubTask2(){
    int ans=0;
    for(int i=0;i<n;i++){
        pair<pair<int,int>,int> res[n];
        for(int j=0;j<n;j++)res[j]={{0,0},0};
        deque<int> q;
        q.pb(i);
        bool was2[N];
        for(int j=0;j<n;j++)was2[j]=0;
        was2[i]=1;
        while(!q.empty()){
            int x=q[0];
            for(auto e:E[x]){
                if(res[e.first].first.first==0&&res[e.first].first.second==0&&res[e.first].second==0){
                    res[e.first]=res[x];
                    if(e.second=='S')res[e.first].first.first++;
                    if(e.second=='I')res[e.first].first.second++;
                    if(e.second=='O')res[e.first].second++;
                }
                if(!was2[e.first])was2[e.first]=1,q.pb(e.first);
            }
            q.pop_front();
        }
        for(int j=i+1;j<n;j++){
            //if(res[j].first.first==res[j].first.second&&res[j].first.second==res[j].second)printf("%i %i\n",i+1,j+1);
            if(res[j].first.first==res[j].first.second&&res[j].first.second==res[j].second)ans++;
        }
    }
    printf("%i",ans);
}
vector<char> v;
bool was[N];
void DFS3(int c){
    was[c]=1;
    for(auto edge:E[c]){
        if(!was[edge.first]){
            v.pb(edge.second);DFS3(edge.first);
        }
    }
}
void SubTask3(){
    int root=0;
    for(int i=0;i<n;i++)if((int)E[i].size()==1)root=i;
    DFS3(root);
    long long ans=0;
    map<long long,int> cnt;
    cnt[0]=1;
    long long a=0,b=0,d=0;
    for(char c:v){
        a+=c=='S',b+=(c=='I'),d+=(c=='O');
        ans+=cnt[(a+b*N+d*N*2)%(3*N+1)];
        cnt[(a+b*N+d*N*2)%(3*N+1)]++;
        //printf("%lld %lld %lld\n",a,b,d);

    }
    printf("%lld",ans);
}
int main(){
    scanf("%i",&n);
    int cnt=0;
    for(int i=0;i<n-1;i++){
        scanf("%i %i %c",&x[i],&y[i],&c[i]);
        --x[i],--y[i];
        E[x[i]].pb({y[i],c[i]});E[y[i]].pb({x[i],c[i]});
        cnt+=c[i]=='S';
    }
    if(cnt==1){
        SubTask1();
        return 0;
    }
    if(n<=1000){
        SubTask2();
    }else SubTask3();
    return 0;
}