#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mn=3*1e5+100;
vector<vector<pair<ll,char > > > graf(mn);
const ll mnn=1050;
ll dp[mnn][mnn][3];
ll dinamik[mn][3];
void dfsdp(ll node,ll t,ll prev)
{
    for(ll i=0;i<graf[t].size();i++)
    {
        if(graf[t][i].first==prev) continue;
        for(ll k=0;k<3;k++) dp[node][graf[t][i].first][k]=dp[node][t][k];
        if(graf[t][i].second=='S')
        {
            dp[node][graf[t][i].first][0]++;
        }
        else if(graf[t][i].second=='I')
        {
            dp[node][graf[t][i].first][1]++;
        }
        else
        {
            dp[node][graf[t][i].first][2]++;
        }
        dfsdp(node,graf[t][i].first,t);
    }
}
int  main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    ll n;
    cin>>n;
    ll kolkos=0;
    pair<ll,ll> ss;
    bool niz=true;
    for(ll i=0;i<n-1;i++)
    {
        ll a,b;
        cin>>a>>b;
        if(a!=b+1 && b!=a+1) niz=false;
        char ch;
        cin>>ch;
        if(ch=='S')
        {
            kolkos++;
            ss.first=a; ss.second=b;
        }
        graf[a].push_back(make_pair(b,ch));
        graf[b].push_back(make_pair(a,ch));
    }
    if(kolkos==1)
    {
        ll num=0;
        queue<ll> q;
        q.push(ss.second);
        ll node=q.front();
        q.pop();
        for(ll i=0;i<graf[node].size();i++)
        {
            if(graf[node][i].second=='I') q.push(graf[node][i].first);
        }
        while(!q.empty())
        {
            node=q.front();
            q.pop();
            for(ll i=0;i<graf[node].size();i++)
            {
                if(graf[node][i].second=='O') num++;
            }
        }
        cout<<num<<endl;
    }
    else if(n<=1000)
    {
        for(ll i=1;i<=n;i++)
        {
            dp[i][i][0]=0;
            dp[i][i][1]=0;
            dp[i][i][2]=0;
            dfsdp(i,i,i);
        }
        ll kolko1=0;
        for(ll i=1;i<=n;i++)
        {
            for(ll j=i+1;j<=n;j++)
            {
                if(dp[i][j][0]==dp[i][j][1] && dp[i][j][1]==dp[i][j][2]) kolko1++;
            }
        }
        cout<<kolko1<<endl;
    }
    else if(niz)
    {
        ll kolko=0;
        ll ts=0;
        ll ti=0;
        ll to=0;
        multiset<pair<ll,ll> > ms;
        ms.insert(make_pair(0,0));
        for(ll i=1;i<=n-1;i++)
        {
            //cout<<i<<endl;
            char sta;
            if(graf[i][0].first==i+1) sta=graf[i][0].second;
            else sta=graf[i][1].second;
            if(sta=='I') ti++;
            else  if(sta=='S') ts++;
            else to++;
            //cout<<sta<<endl;
            kolko+=ms.count(make_pair(ts-to,ts-ti));
            ms.insert(make_pair(ts-to,ts-ti));
        }
        cout<<kolko<<endl;
    }
}