#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define xx first
#define yy second
#define srt(a) sort(a.begin(),a.end());
#define srtg(a,int) sort(a.begin(),a.end(),greater<int>())
#define lb(a,x) lower_bound(a.begin(),a.end(),x)
#define up(a,x) upper_bound(a.begin(),a.end(),x)
#define fnd(a,x) find(a.begin(),a.end(),x)
#define vstart auto startt=chrono::system_clock::now()
#define vend auto endd=chrono::system_clock::now()
#define vvreme chrono::duration<double> vremee=endd-startt
#define ios ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define mt make_tuple

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef string str;
typedef tuple<int,int,int> tiii;

const int maxn=3e5+2;
const int logg=21;

deque<int> tren;
vector<pair<int,char> > g[maxn];
tiii razlike[maxn];
bool bio[maxn];
pii vremena[maxn];
int vreme=0;
int lift[maxn][logg];

void dfs(int cvor,int S,int I,int O){
    if(bio[cvor]) return;
    bio[cvor]=true;
    vreme++;
    vremena[cvor].xx=vreme;
    get<0>(razlike[cvor])=S-I;
    get<1>(razlike[cvor])=S-O;
    get<2>(razlike[cvor])=I-O;
    for(int i=0;i<logg;i++){
        if((int)tren.size()<((int)1<<i)) lift[cvor][i]=-1;
        else lift[cvor][i]=tren[(int)tren.size()-((int)1<<i)];
    }
    tren.pb(cvor);
    for(pair<int,char> i:g[cvor]){
        dfs(i.xx,S+(i.yy=='S' ? 1:0),I+(i.yy=='I' ? 1:0),O+(i.yy=='O' ? 1:0));
    }
    tren.pop_back();
    vreme++;
    vremena[cvor].yy=vreme;
}

bool iznad(int a,int b){
    if(vremena[a].xx<=vremena[b].xx && vremena[b].yy<=vremena[a].yy) return true;
    return false;
}

int lca(int a,int b){
    for(int i=logg-1;i>=0;i--){
        if(lift[b][i]==-1) continue;
        //cout<<"lca: "<<b<<" "<<i<<" "<<lift[b][i]<<"\n";
        if(!iznad(lift[b][i],a)) b=lift[b][i];
    }
    return lift[b][0];
}

bool ok(tiii a,tiii b,tiii c){
    if(get<0>(a)+get<0>(b)-get<0>(c)==0 && get<1>(a)+get<1>(b)-get<1>(c)==0
       && get<2>(a)+get<2>(b)-get<2>(c)==0) return true;
    return false;
}

void dfs3(int cvor,int prosli,int jos,int S,int I,int O,int &res){
    //cout<<"  novi cvor: "<<cvor<<"\n";
    if(S==1 && I==1 && O==1) {res++; cout<<"evo\n";}
    if(jos==0) return;
    for(pair<int,char> i:g[cvor]){
        if(i.xx!=prosli)
        dfs3(i.xx,cvor,jos-1,S+(i.yy=='S' ? 1:0),I+(i.yy=='I' ? 1:0),O+(i.yy=='O' ? 1:0),res);
    }
}

void dfs2(int cvor,int prosli,int jos,int &res){
    //cout<<"cvor: "<<cvor<<"\n";
    if(bio[cvor]) return;
    bio[cvor]=true;
    dfs3(cvor,-1,3,0,0,0,res);
    if(jos==0) return;
    for(pair<char,int> i:g[cvor]){
        if(i.xx!=prosli && !bio[i.xx]) dfs2(i.xx,cvor,jos-1,res);
    }
}

void solve(){
    int n;
    cin>>n;
    bool linija=true;
    bool binarno=true;
    int koliko=0;
    int koji=0;
    for(int i=0;i<n-1;i++){
        int a,b;
        char c;
        cin>>a>>b>>c;
        if(max(a,b)!=min(a,b)+1) linija=false;
        if(max(a,b)/2!=min(a,b)) binarno=false;
        if(c=='S'){
            koliko++;
            koji=a;
        }
        g[b].pb({a,c});
        g[a].pb({b,c});
    }
    if(linija){
        map<tiii,int> nesto;
        int S=0;
        int I=0;
        int O=0;
        int res=0;
        for(int i=1;i<=n;i++){
            //cout<<i<<"\n";
            if(i>2){
                S+=(max(g[i-1][0],g[i-1][1]).yy=='S');
                I+=(max(g[i-1][0],g[i-1][1]).yy=='I');
                O+=(max(g[i-1][0],g[i-1][1]).yy=='O');
            }else if(i>1){
                S+=g[i-1][0].yy=='S';
                I+=g[i-1][0].yy=='I';
                O+=g[i-1][0].yy=='O';
            }
            //cout<<S-I<<" "<<O-S<<" "<<O-I<<"\n";
            res+=nesto[mt(S-I,S-O,I-O)];
            nesto[mt(S-I,S-O,I-O)]++;
        }
        cout<<res;
    }else if(binarno){

    }else if(koliko==1){
        int res=0;
        //cout<<"ovde\n";
        dfs2(koji,-1,5,res);
        cout<<res/2<<"\n";
    }else{
        dfs(1,0,0,0);
        /*for(int i=1;i<=n;i++){
            cout<<get<0>(razlike[i])<<" "<<get<1>(razlike[i])<<" "<<get<2>(razlike[i])<<"\n";
        }*/
        int res=0;
        for(int i=1;i<=n;i++){
            for(int j=i+1;j<=n;j++){
                //cout<<i<<" "<<j<<" "<</*lca(i,j)<<*/"\n";
                if(ok(razlike[i],razlike[j],razlike[lca(i,j)])) res++;
            }
        }
        cout<<res;
    }
}

int main(){
    ios;
    int t=1;
    //cin>>t;
    while(t--) solve();
    return 0;
}
/*
7
1 2 S
2 3 S
3 4 I
4 5 O
5 6 O
6 7 I

12
12 11 I
11 10 O
10 3 O
3 4 S
4 5 O
5 6 I
3 2 I
2 1 O
3 7 I
7 8 I
8 9 I
*/