#include <bits/stdc++.h>

using namespace std;

vector<bool> visited;
vector<pair<int,char>> K[300000];
int counter=0;

void dfs(int i, int S, int I, int O)
{

    int j;
    if(visited[i])return;
    visited[i]=true;
    if(S>0 && S==I && S==O)counter++;
    for(j=0;j<K[i].size();j++)
    {
        if(K[i][j].second=='S')dfs(K[i][j].first,S+1,I,O);
        else if(K[i][j].second=='I')dfs(K[i][j].first,S,I+1,O);
        else dfs(K[i][j].first,S,I,O+1);
    }
}

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    int i,a,b,A,B,R,n,j,k=0,index,t;
    char c,c1,c2;
    cin>>n;
    vector<char> CH(n-1);
    for(i=0;i<n-1;i++)
    {
        cin>>a>>b>>c;
        if(c=='S')
        {
            k++;
            A=a-1;
            B=b-1;
        }
        CH[min(a,b)-1]=c;
        K[a-1].push_back({b-1,c});
        K[b-1].push_back({a-1,c});
        visited.push_back(false);
    }
    visited.push_back(false);
    if(k==1)
    {
        for(i=0;i<K[A].size();i++)
        {
            c1=K[A][i].second;
            if(c1!='S')
            {
                R=K[A][i].first;
                for(j=0;j<K[R].size();j++)
                {
                    if(K[R][j].second=='I' && c1=='O')counter++;
                    if(K[R][j].second=='O' && c1=='I')counter++;
                }
            }
            for(j=0;j<K[B].size();j++)
            {
                if(K[B][j].second=='I' && c1=='O')counter++;
                if(K[B][j].second=='O' && c1=='I')counter++;
            }
        }
        for(i=0;i<K[B].size();i++)
        {
            c1=K[B][i].second;
            if(c1!='S')
            {
                R=K[B][i].first;
                for(j=0;j<K[R].size();j++)
                {
                    if(K[R][j].second=='I' && c1=='O')counter++;
                    if(K[R][j].second=='O' && c1=='I')counter++;
                }
            }
        }
        cout<<counter;
    }
    else if(n<=1000)
    {

        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)visited[j]=false;
            dfs(i,0,0,0);
        }
        cout<<counter/2;
    }
    else
    {
        map<int,int> M;
        vector<int> S(n),I(n),O(n);
        S[0]=0;I[0]=0;O[0]=0;
        for(i=0;i<n-1;i++)
        {
            if(CH[i]=='S')
            {
                S[i+1]=S[i]+1;
                I[i+1]=I[i];
                O[i+1]=O[i];
            }
            else if(CH[i]=='I')
            {
                S[i+1]=S[i];
                I[i+1]=I[i]+1;
                O[i+1]=O[i];
            }
            else
            {
                S[i+1]=S[i];
                I[i+1]=I[i];
                O[i+1]=O[i]+1;
            }
        }
        for(i=0;i<n;i++)
        {
            t=min(min(S[i],I[i]),O[i]);
            S[i]-=t;
            I[i]-=t;
            O[i]-=t;
            k=S[i]*100+I[i]*10+O[i];
            counter+=M[k];
            M[k]++;
        }
        cout<<counter;
    }
    return 0;
}