#include <bits/stdc++.h>
#define fi first
#define se second


using namespace std;

int main()
{
    int n,ni=0,nj=0;
    long long res=0;
    cin>>n;

    vector<int> NSbranches(n,0);
    vector<int> NObranches(n,0);
    vector<int> NIbranches(n,0);

    vector<pair<int,int> > Ipairs(n);
    vector<pair<int,int> > Opairs(n);

    int a,b;
    char c;
    for(int i=0;i<n-1;i++){

        cin>>a>>b>>c;
        if(c=='S'){NSbranches[a-1]++;NSbranches[b-1]++;}
        else if(c=='O'){NObranches[a-1]++;NObranches[b-1]++;Opairs[nj++]=make_pair(a-1,b-1);}
        else{Ipairs[ni++]=make_pair(a-1,b-1);NIbranches[a-1]++;NIbranches[b-1]++;}
    }
    for(int i=0;i<ni;i++){
        res+=(long long)NSbranches[Ipairs[i].fi]*NObranches[Ipairs[i].se]+(long long)NObranches[Ipairs[i].fi]*NSbranches[Ipairs[i].se];
    }
    for(int i=0;i<nj;i++){
        res+=(long long)NSbranches[Opairs[i].fi]*NIbranches[Opairs[i].se]+(long long)NIbranches[Opairs[i].fi]*NSbranches[Opairs[i].se];
    }
    cout << res << endl;
    return 0;
}