#include <bits/stdc++.h>
using namespace std;
int main() {
	ios::sync_with_stdio(false); cin.tie(0);
	int U, V, N, S=0, I=0, O=0, s=0, i=0, o=0, Min, MIn, a, b=0, A, B, k, rez=0;
	char c, MIN;
	map<int, char> x;
	cin>>N;
	for(int i=0; i<N-1; i++) {
		cin>>U>>V>>c;
		x[min(U, V)]=c;
		switch(c) {
			case 'S':
				S++;
				break;
			case 'I':
				I++;
				break;
			case 'O':
				O++;
				break;
		}
	}
	Min=min(S, min(I, O));
	if(Min==S)
	MIN='S';
	else if(Min==I)
	MIN='I';
	else
	MIN='O';
	Min++;
	while(Min!=0) {
		MIn=0;
	for(int i=1; MIn==0; i++) {
		if(x[i]==MIN) {
			a=i;
			MIn++;
			switch(x[i]) {
				case 'S':
					s++;
					break;
				case 'I':
					i++;
					break;
				case 'O':
					o++;
					break;
			}
		}
	}
	Min--;
	while(a!=b) {
		s=0;
		i=0;
		o=0;
		MIn=0;
		for(int i=a+1; MIn!=Min; i++) {
			if(x[i]==MIN)
			MIn++;
			switch(x[i]) {
				case 'S':
					s++;
					break;
				case 'I':
					i++;
					break;
				case 'O':
					o++;
					break;
			}
			if(MIn==Min)
			b=i;
		}
		A=a;
		B=b;
		while(min(s, min(i, o))<MIn) {
			k=0;
			if((x[A-1]=='S'&&s<MIn)||(x[A-1]=='I'&&i<MIn)||(x[A-1]=='O'&&o<MIn)) {
				A--;
				switch(x[A]) {
				case 'S':
					s++;
					break;
				case 'I':
					i++;
					break;
				case 'O':
					o++;
					break;
				}
			}
			else
			k++;
			if((x[B+1]=='S'&&s<MIn)||(x[B+1]=='I'&&i<MIn)||(x[B+1]=='O'&&o<MIn)) {
				B++;
				switch(x[B]) {
				case 'S':
					s++;
					break;
				case 'I':
					i++;
					break;
				case 'O':
					o++;
					break;
				}
			}
			else
			k++;
			if(k==2)
			break;
		}
		if(!(max(s, max(i, o))>MIn||k==2))
		rez++;
		for(int i=a+1; x[i]==x[a]; i++) {
			if(x[i]==x[a])
			a=i;
		}
	}
	}
	cout<<rez;
	return 0;
}