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

int s1, s2;
bool flag = 1;
int n, ans;
int cnt[256];
bool vis[1005];
int res[1005][1005];
vector<pair<int, char>> adj[1005];
map< tuple<int, int, int>, int > mp;

void dfs(int u, int v, int s, int i, int o) {
	if (s && s == i && i == o) {
		res[u][v] = 1;
		res[v][u] = 1;
	}
	vis[v] = 1;
	for (auto it : adj[v]) {
		if (!vis[it.first]) {
			dfs(u, it.first, s + (it.second == 'S'), i + (it.second == 'I'), o + (it.second == 'O'));
		}
	}
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin >> n;
	for (int i = 0; i < n - 1; i++) {
		int u, v;
		char c;
		cin >> u >> v >> c;
		if (v < u) swap(u, v);
		flag &= (v == u + 1);
		cnt[c]++;
		if (c == 'S') {
			s1 = u;
			s2 = v;
		}
		adj[u].push_back({v, c});
		adj[v].push_back({u, c});
	}
	if (cnt['S'] == 1) {
		int i = 0;
		int o = 0;
		for (auto it : adj[s1]) {
			if (it.second == 'O') o++;
		}
		for (auto it : adj[s2]) {
			if (it.second == 'I') i++;
		}
		ans += i * o;
		i = 0;
		o = 0;
		for (auto it : adj[s1]) {
			if (it.second == 'I') i++;
		}
		for (auto it : adj[s2]) {
			if (it.second == 'O') o++;
		}
		ans += i * o;
		for (auto it : adj[s1]) {
			for (auto jt : adj[it.first]) {
				if (it.second == 'I' && jt.second == 'O') ans++;
				if (it.second == 'O' && jt.second == 'I') ans++;
			}
		}
		for (auto it : adj[s2]) {
			for (auto jt : adj[it.first]) {
				if (it.second == 'I' && jt.second == 'O') ans++;
				if (it.second == 'O' && jt.second == 'I') ans++;
			}
		}
		cout << ans << '\n';
	} else if (flag) {
		int S = 0, I = 0, O = 0;
		for (int i = 2; i <= n; i++) {
			for (auto it : adj[i]) {
				if (it.first == i - 1) {
					bool s1 = (it.second == 'S');
					bool i1 = (it.second == 'I');
					bool o1 = (it.second == 'O');
					S += s1;
					I += i1;
					O += o1;
					ans += mp[make_tuple(S - 1, I - 1, O - 1)];
					if (S == I && I == O) ans++;
					mp[make_tuple(S, I, O)] = mp[make_tuple(S - 1, I - 1, O - 1)] + 1;
				}
			}
		}
		cout << ans << '\n';
	} else if (n <= 1200) {
		for (int i = 1; i <= n; i++) {
			memset(vis, 0, sizeof vis);
			dfs(i, i, 0, 0, 0);
		}
		for (int i = 1; i <= n; i++) {
			for (int j = i + 1; j <= n; j++) {
				if (res[i][j]) {
					ans++;
				}
			}
		}
		cout << ans << '\n';
	}
	return 0;
}