#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define all(a) a.begin(), a.end()
#define ff(i,a,b) for(int i=a;i<=b;i++)
#define fb(i,b,a) for(int i=b;i>=a;i--)

using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 300005;
const int inf = 1e9 + 5;

int n;

vector<pii> g[maxn];

int fun(char ch){
   return (ch == 'S' ? 0 : (ch == 'I' ? 1 : 2));
}

pii koji;

int kol[maxn][3];
void dfs1(int v, int p, int pw){
   kol[v][pw] += 1;
   for(auto c : g[v]){
      int u = c.fi;
      int w = c.se;
      if(u != p){
         kol[u][1] += kol[v][1];
         kol[u][2] += kol[v][2];
         dfs1(u, v, w);
      }
   }
}

void sub_task1(){
//   cout << koji.fi << " " << koji.se << endl;
   int a = koji.fi;
   int b = koji.se;
   for(auto c : g[a]){
      if(c.fi != b)dfs1(c.fi, a, c.se);
   }
   for(auto c : g[b]){
      if(c.fi != a)dfs1(c.fi, b, c.se);
   }
//   cout << endl;
//   ff(i,1,n)cout << kol[i][1] << " " << kol[i][2] << endl;
   int rez = 0;
   ff(i,1,n)rez += (kol[i][1] == kol[i][2]);
   int ai = 0;
   int ao = 0;
   int bi = 0;
   int bo = 0;
   for(auto c : g[a]){
      ai += (c.se == 1);
      ao += (c.se == 2);
   }
   for(auto c : g[b]){
      bi += (c.se == 1);
      bo += (c.se == 2);
   }
//   cout << endl;
//   cout << ai << " " << ao << endl;
//   cout << bi << " " << bo << endl;
   rez += (ai * bo) + (ao * bi);
   cout << rez - 2 << endl;
   exit(0);
}

int ima[maxn][3];
void dfs2(int v, int p, int pw){
   ima[v][pw] += 1;
   for(auto c : g[v]){
      int u = c.fi;
      int w = c.se;
      if(u != p){
         ima[u][0] += ima[v][0];
         ima[u][1] += ima[v][1];
         ima[u][2] += ima[v][2];
         dfs2(u, v, w);
      }
   }
}

// redom
map<array<int,3>, int> bio;
void sub_task3(){
   for(auto c : g[1])dfs2(c.fi, 1, c.se);
   int rez = 0;
   ff(i,1,n){
      int a = ima[i][0];
      int b = ima[i][1];
      int c = ima[i][2];
      if(bio[{a - 1, b - 1, c - 1}])rez += (i - bio[{a - 1, b - 1, c - 1}] == 3);
      bio[{a, b, c}] = i;
   }
   cout << rez << endl;
   exit(0);
}

int sta[maxn][3];
void dfs3(int v, int p, int pw){
   sta[v][pw] += 1;
   for(auto c : g[v]){
      int u = c.fi;
      int w = c.se;
      if(u != p){
         sta[u][0] += sta[v][0];
         sta[u][1] += sta[v][1];
         sta[u][2] += sta[v][2];
         dfs3(u, v, w);
      }
   }
}

int timer = 0;
int d[maxn];
int in[maxn];
int out[maxn];
int deda[maxn][20];
void dfs(int v, int p){
   deda[v][0] = p;
   ff(i,1,19)deda[v][i] = deda[deda[v][i - 1]][i - 1];
   in[v] = ++timer;
   for(auto c : g[v]){
         int u = c.fi;
         int w = c.se;
         if(u != p){
            d[u] = d[v] + 1;
            dfs(u, v);
         }
   }
   out[v] = timer;
}
bool predak(int u, int v){
    return (in[u] <= in[v] && out[u] >= out[v]);
}
int lca(int u, int v){
    if(predak(u, v))return u;
    if(predak(u, v))return v;
    for(int i=19;i>=0;i--){
        if(deda[u][i] && !predak(deda[u][i], v))u = deda[u][i];
    }
    return deda[u][0];
}
int dist(int u, int v){
    return d[u] + d[v] - 2 * d[lca(u, v)];
}

// n <= 1000
void sub_task2(){
   dfs(1, 0);
   for(auto c : g[1])dfs3(c.fi, 1, c.se);
//   cout << endl;
//   ff(i,1,n){
//      ff(j,0,2)cout << sta[i][j] << " ";
//      cout << endl;
//   }
   int rez = 0;
   ff(i,1,n){
      ff(j,i + 1, n){
         int x = lca(i, j);
//         cout << "x: " << x << endl;
//         cout << "i: " << i << endl;
//         cout << "j: " << j << endl;
//         ff(k,0,2)cout << sta[i][k] << " ";
//         cout << endl;
//         ff(k,0,2)cout << sta[j][k] << " ";
//         cout << endl;
//         ff(k,0,2)cout << sta[x][k] << " ";
//         cout << endl;
         int a0 = sta[i][0] - sta[x][0];
         int a1 = sta[i][1] - sta[x][1];
         int a2 = sta[i][2] - sta[x][2];
         int b0 = sta[j][0] - sta[x][0];
         int b1 = sta[j][1] - sta[x][1];
         int b2 = sta[j][2] - sta[x][2];
//         cout << "i: " << i << endl;
//         cout << "j: " << j << endl;
//         cout << "a0: " << a0 << endl;
//         cout << "a1: " << a1 << endl;
//         cout << "a2: " << a2 << endl;
//         cout << "b0: " << b0 << endl;
//         cout << "b1: " << b1 << endl;
//         cout << "b2: " << b2 << endl;
         int c0 = a0 + b0;
         int c1 = a1 + b1;
         int c2 = a2 + b2;
//         cout << c0 << " " << c1 << " " << c2 << endl;
         if(c0 == c1 && c1 == c2 && c0 > 0 && c1 > 0 && c2 > 0)rez += 1;
//         cout << endl;
//         cout << endl;
      }
//      cout << endl;
   }
   cout << rez << endl;
}

int main()
{
   ios::sync_with_stdio(false);
   cout.tie(nullptr);
   cin.tie(nullptr);
   cin >> n;
   bool redom = 1;
   int br = 0;
   ff(i,1,n - 1){
      int u, v;
      char ch;
      cin >> u >> v >> ch;
      if(u > v)swap(u, v);
      int w = fun(ch);
      g[u].pb({v, w});
      g[v].pb({u, w});
      if(u + 1 != v)redom = 0;
      if(w == 0){
         br++;
         koji = {u, v};
      }
   }
   if(redom)sub_task3();
   if(br == 1)sub_task1();
   sub_task2();
   return 0;
}
/**

10
1 2 O
2 3 I
3 4 O
4 5 I
3 6 S
6 7 I
7 8 O
6 9 O
9 10 I

7
1 2 S
2 3 I
3 4 O
4 5 I
5 6 S
6 7 O

6
4 1 S
5 6 O
1 5 I
1 2 S
3 1 S

6
1 2 S
2 3 0
2 4 S
4 6 I
3 5 I
**/