#include <bits/stdc++.h>

using namespace std;

bool sio(string str){
    int s = 0, i = 0, o = 0;
    for(int k = 0; k < str.size(); k++){
        if(str[k] == 'S')
            s++;
        if(str[k] == 'I')
            i++;
        if(str[k] == 'O')
            o++;
    }
    if(s == i && i == o && s != 0)
        return true;
    return false;
}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    const int maksimum = 1000000;
    int n;
    cin >> n;
    vector<vector<int> > graf (n, vector<int> (n, maksimum));
    vector<vector<string> > putevi (n, vector<string> (n, ""));

    for(int i = 0; i < n; i++){
        graf[i][i] = 0;
    }

    for(int i = 0; i < n - 1; i++){
        int a, b;
        string slovo;
        cin >> a >> b >> slovo;
        graf[a-1][b-1] = 1;
        graf[b-1][a-1] = 1;
        putevi[a-1][b-1] += slovo;
        putevi[b-1][a-1] += slovo;
    }

    for(int k = 0; k < n; k++){
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                if(graf[i][j] > graf[i][k] + graf[k][j]){
                    putevi[i][j] = putevi[i][k] + putevi[k][j];
                }
                graf[i][j] = min(graf[i][j], graf[i][k] + graf[k][j]);
            }
        }
    }
    int resenje = 0;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            if(sio(putevi[i][j]))
                resenje++;
        }
    }
    cout << resenje / 2;
}