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


using namespace std;

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