#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define ff first
#define ss second
using namespace std;
struct slog{
ll sc,ic,oc;
bool operator<(const slog &a)const{
return (sc>a.sc)or(sc==a.sc and ic>a.ic)or(sc==a.sc and ic==a.ic and oc>a.oc);
}
bool operator==(const slog &a)const{
return (sc==a.sc and ic==a.ic and oc==a.oc);
}
}a[300005],t,t2;
struct comp{
template<typename T>
bool operator()(const T &l, const T &r) const{
    if (l.first==r.first)
        return l.second>r.second;
        return l.first<r.first;
    }
};
pair<slog,ll>p2;
ll n,x,y,res,mx,mn;
pair<ll,ll> grupa[300005];
char sl;
vector<pair<ll,char>>v[300005];
queue<ll>q;
pair<ll,char>p;
map<pair<slog,ll>,ll,comp>m,m2;
bool posecen[300005];
void bfs(int x){
int fr,br=1;
q.push(x);
posecen[x]=true;
grupa[x].ff=0;
grupa[x].ss=x;
  fr=x;
    for (int i=0;i<v[x].size();i++){
        if(!posecen[v[fr][i].ff]){
                q.push(v[fr][i].ff);
                a[v[fr][i].ff].sc=a[fr].sc;
                a[v[fr][i].ff].ic=a[fr].ic;
                a[v[fr][i].ff].oc=a[fr].oc;
                if(v[fr][i].ss=='S'){a[v[fr][i].ff].sc++;}
                else if(v[fr][i].ss=='I'){a[v[fr][i].ff].ic++;}
                else{a[v[fr][i].ff].oc++;}
                posecen[v[fr][i].ff]=true;
                grupa[v[fr][i].ff].ff=br;
                grupa[v[fr][i].ff].ss=v[fr][i].ff;
                br++;
        }
    }
    q.pop();
while(q.size()){
    fr=q.front();
    for(int i=0;i<v[fr].size();i++){
        if(!posecen[v[fr][i].ff]){
                q.push(v[fr][i].ff);
                a[v[fr][i].ff].sc=a[fr].sc;
                a[v[fr][i].ff].ic=a[fr].ic;
                a[v[fr][i].ff].oc=a[fr].oc;
                if(v[fr][i].ss=='S'){a[v[fr][i].ff].sc++;}
                else if(v[fr][i].ss=='I'){a[v[fr][i].ff].ic++;}
                else{a[v[fr][i].ff].oc++;}
                posecen[v[fr][i].ff]=true;
                grupa[v[fr][i].ff].ff=grupa[fr].ff;
                grupa[v[fr][i].ff].ss=v[fr][i].ff;
        }
    }
    q.pop();
}
}
int main () {
cin.tie(NULL);
ios_base::sync_with_stdio(false);
cin>>n;
for (int i=1;i<n;i++){
    cin>>x>>y>>sl;
    p=make_pair(x,sl);
    v[y].pb(p);
    p=make_pair(y,sl);
    v[x].pb(p);
}
bfs(1);
sort(grupa+1,grupa+n+1);
for(int i=1;i<=n;i++){
    if(grupa[i].ff!=grupa[i-1].ff){
        m.clear();
        t.sc=0;
        t.ic=0;
        t.oc=0;
        p2=make_pair(t,grupa[i].ff);
        m[p2]++;
    }
    t.sc=a[grupa[i].ss].sc-a[grupa[i].ss].ic;
    t.ic=a[grupa[i].ss].ic-a[grupa[i].ss].oc;
    t.oc=a[grupa[i].ss].oc-a[grupa[i].ss].sc;
    p2=make_pair(t,grupa[i].ff);
    m[p2]++;
    if(m[p2]>1){
        res+=m[p2]-1;
    }
    p2=make_pair(t,-1);
    if(m2[p2]>=1){
        res+=m2[p2];
    }
    t2.sc=t.sc*-1;
    t2.ic=t.ic*-1;
    t2.oc=t.oc*-1;
    p2=make_pair(t,-1);
    m2[p2]++;
}
if(grupa[2].ff==grupa[n].ff){
    res/=2;
}
cout<<res<<endl;
return 0;
}