#include <bits/stdc++.h>
#define K int(1e9 + 10)
#define INF int(1e9)
#define MOD (long long)(1e9 + 7)
#define endl "\n"
using namespace std;
typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;

vector<vector<pair<int, char>>> t;
vector<vector<int>> s, dp;
map<tuple<int, int, int>, int> m, lst;
int ans = 0;

void dfs(int a, int l, char x){
    s[a][0] = s[l][0] + (x == 'S');
    s[a][1] = s[l][1] + (x == 'I');
    s[a][2] = s[l][2] + (x == 'O');
    int dec = min({s[a][0], s[a][1], s[a][2]}),
    max_ = max({s[a][0], s[a][1], s[a][2]});
    s[a][0] -= dec, s[a][1] -= dec, s[a][2] -= dec;
    ans += m[{max_ - s[a][0], max_ - s[a][1], max_ - s[a][2]}] + lst[{s[a][0], s[a][1], s[a][2]}];
    ++lst[{s[a][0], s[a][1], s[a][2]}];
    for (auto i : t[a])
        if (i.first != l) dfs(i.first, a, i.second);
    ++m[make_tuple(s[a][0], s[a][1], s[a][2])];
    --lst[{s[a][0], s[a][1], s[a][2]}];
}

void solve(){
    int n;
    cin >> n;   
    t.resize(n);
    s.resize(n, vector<int>(3));
    int a, b; char x;
    for (int i = 0; i < n - 1; i++){
        cin >> a >> b >> x;
        --a, --b;
        t[a].push_back({b, x});
        t[b].push_back({a, x});
    }
    dfs(0, 0, 'X');
    cout << ans << endl;
}
 
int main(){
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
 
    int t = 1;
    //cin >> t;
    while (t--)
        solve();
 
    return 0;
}