#include <bits/stdc++.h>

#define ll long long
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define MAXN 300010

using namespace std;

vector< pair<int, char> > graph[MAXN];
bool visited[MAXN];
ll ans = 0;

void tst1(int u, int v) {

    ll sol = 0, i1 = 0, o1 = 0;
    for(auto k: graph[u]) {
        if(k.second == 'I') {
            i1++;
            for(auto l: graph[k.first])
                if(l.second == 'O')
                    sol++;
        } else if(k.second == 'O') {
            o1++;
            for(auto l: graph[k.first])
                if(l.second == 'I')
                    sol++;
        }
    }
    ll i2 = 0, o2 = 0;
    for(auto k: graph[v]) {
        if(k.second == 'I') {
            i2++;
            for(auto l: graph[k.first])
                if(l.second == 'O')
                    sol++;
        } else if(k.second == 'O') {
            o2++;
            for(auto l: graph[k.first])
                if(l.second == 'I')
                    sol++;
        }
    }
    sol += i1 * o2 + i2 * o1;
    cout << sol << endl;
}

void dfs(int u, int x, int s, int i, int o) {

    visited[u] = true;
    if(s == i && i == o && u > x)
        ans++;
    
    for(auto v: graph[u]) {
        if(!visited[v.first]) {
            if(v.second == 'S')
                dfs(v.first, x, s + 1, i, o);
            else if(v.second == 'I')
                dfs(v.first, x, s, i + 1, o);
            else
                dfs(v.first, x, s, i, o + 1);
        }
    }
}

int main() {

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

    int n;
    cin >> n;

    int brS = 0, uS = -1, vS = -1;
    bool testcase3 = true;
    for(int i = 0; i < n - 1; i++) {
        int u, v;
        char c;
        cin >> u >> v >> c;
        u--; v--;

        if(abs(u - v) != 1)
            testcase3 = false;
        if(c == 'S') {
            brS++;
            uS = u;
            vS = v;
        }
        
        graph[u].eb(v, c);
        graph[v].eb(u, c);
    }

    if(testcase3) {
        char a[n - 1];
        for(int i = 0; i < n - 1; i++)
            a[i] = (graph[i][0].first == i + 1) ? graph[i][0].second : graph[i][1].second;

        pair<int, int> now(0, 0);
        map< pair<int, int>, ll > cnt;
        cnt[now]++;
        ll sol = 0;
        for(int i = 0; i < n - 1; i++) {
            if(a[i] == 'S') {
                now.first--;
                now.second--;
            } else if(a[i] == 'I')
                now.first++;
            else
                now.second++;
            sol += cnt[now];
            cnt[now]++;
        }
        cout << sol << endl;
        return 0;
    } else if(brS == 1) {
        tst1(uS, vS);
        return 0;
    }

    for(int i = 0; i < n - 1; i++) {
        memset(visited, false, sizeof(visited));
        dfs(i, i, 0, 0, 0);
    }
    cout << ans << endl;
    return 0;
}