#include <bits/stdc++.h>
using namespace std;

const int N = 300005;
vector<pair<int, int>> graph[N];

int Sdfs(int v, int p, int cnto, int cnti){
    if(cnti == 1 && cnto == 1)
        return 1;
    if(cnti > 1 || cnto > 1)
        return 0;
    if(graph[v].size() == 1)
        return 0;
    int res = 0;
    for(auto u : graph[v]){
        if(u.first == p || u.second == 'S')
            continue;
        res += Sdfs(u.first, p, cnto + (u.second == '0'), cnti + (u.second == 'I'));
    }
    return res;
}

void solveS(int sx, int sy){
    int res = 0;
    res += Sdfs(sx, sy, 0, 0);
    res += Sdfs(sx, sy, 0, 0);
    cout << res << "\n";
}

int main()
{
    int n;
    {
        int scount = 0;
        int sx, sy;
        cin >> n;
        for(int i = 0; i < n - 1; ++i){
            int x, y;
            char c;
            cin >> x >> y >> c;
            graph[x].push_back({y, c});
            graph[y].push_back({x, c});
            if(c == 'S'){
                scount++;
                sx = x;
                sy = y;
            }
        }

        if(scount == 1){
            solveS(sx, sy);
            return 0;
        }
    }

    cout << "-1\n";
    return 0;
}