#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

#define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
#define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
#define mod 998244353
#define xx first
#define yy second
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define ll long long
#define pii pair<int,int>
#define maxn 300005

using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>,rb_tree_tag, tree_order_statistics_node_update> ordered_set;/// find_by_order(x)(x+1th) , order_of_key() (strictly less)
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());


int n;
vector<pii> graf[300005];

int br = 1;
int niz[2 * maxn];

void dfs(int x, int y){
    for(auto c:graf[x]){
        if(c.xx == y)continue;
        niz[br++] = c.yy;
        dfs(c.xx, x);
        niz[br++] = -c.yy;
    }
}

int tr[15];
map<pii,int> ima;

int main()
{

    ios_base::sync_with_stdio(false); cin.tie(0);

    cin >> n;
    ff(i,1,n - 1){
        int a,b;
        cin >> a >> b;
        char x;
        cin >> x;
        int val = 1;
        if(x == 'I')val = 2;
        if(x == 'O')val = 3;
        graf[a].pb({b,val});
        graf[b].pb({a,val});
    }
    dfs(1,0);
    br--;
    ima[{0,0}] = 1;
    ll res = 0;
    ff(i,1,br){
        tr[abs(niz[i])]++;
        pii sta = {tr[1] - tr[2], tr[1] - tr[3]};
        if(niz[i] > 0 && ima.find(sta) != ima.end())res += ima[sta];
        if(ima.find(sta) != ima.end())ima[sta]++;
        else ima[sta] = 1;
    }
    cout << res << "\n";
    return 0;
}