#include <iostream>
#include <cmath>
using namespace std;

int main(int argc, char** argv) {
	int x, y, n, k, j, i, o, s, i1;
	cin >> n;
	char c[n][n];
	char c1;
	bool t1 = true;
	k = 0;
	for(int i = 1; i<n; i++)
	{
		cin >> x >> y >> c1;
		if(abs(x-y)!=1) t1 = false;
		c[x][y] = c[y][x] = c1;
	}
	if(t1)
	{
		for(i = 0; i<n-1;i++)
		{
			j = i+1;
			o = s = i1 = 0;
			while((j+i)<(2*(n-1)-1))
			{
				if(c[i][j]=='S') s++;
				if(c[i][j]=='O') o++;
				if(c[i][j]=='I') i1++;
				if(s==o==i1) k++;
			}
		}
	}
	cout << k << endl;
	return 0;
}