#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]++;NSbranches[b]++;}
        else if(c=='O'){NObranches[a]++;NObranches[b]++;Opairs[nj++]=make_pair(a,b);}
        else{Ipairs[ni++]=make_pair(a,b);NIbranches[a]++;NIbranches[b]++;}
    }
    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;
}