#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)

using namespace std;

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

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

deque<int> tren;
vector<pair<int,char> > g[maxn];
pii 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;
    razlike[cvor].xx=S-I;
    razlike[cvor].yy=S-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(pii a,pii b,pii c){
    if(a.xx+b.xx-c.xx==0 && a.yy+b.yy-c.yy==0) return true;
    return false;
}

void solve(){
    int n;
    cin>>n;
    bool linija=true;
    bool binarno=true;
    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;
        g[b].pb({a,c});
        g[a].pb({b,c});
    }
    dfs(1,0,0,0);
    /*for(int i=1;i<=n;i++){
        cout<<razlike[i].xx<<" "<<razlike[i].yy<<"\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;
}