#include <iostream>
//#include <iomanip>
//#include <cmath>
//#include <algorithm>
//#include <string>
#include <vector>
//#include <ctime>
//#include <cstdlib>
//#include <mycpp.h>

using namespace std;

class grana 
{
	public:
		int Ca;
		int Cb;
		char S;
		
		void unos()
		{
			cin>>Ca>>Cb>>S;
		}
		char Slovo()
		{
			return S;
		}
};

vector<int> indexS(grana a[],int n)
{
	vector<int> ind;
	for(int i=0;i<n;i++)
		if(a[i].Slovo()=='S')
			ind.push_back(i);
	return ind;
}

int SIO(grana a[],int n,char c,int i)
{
	int br=0;
	switch(c)
	{
		case 'S':
			{
				for(int j=0;j<n;j++)
				{
					if(j==i || a[j].Slovo()!='I')
						continue;
					if(a[j].Slovo()=='I' && (a[i].Ca==a[j].Ca || a[i].Ca==a[j].Cb || a[i].Cb==a[j].Ca || a[i].Cb==a[j].Cb))
						br+=SIO(a,n,'I',j);
				}
				break;
			}
		case 'I':
			for(int j=0;j<n;j++)
			{
				if(j==i || a[j].Slovo()!='O')
					continue;
				if(a[j].Slovo()=='O' && (a[i].Ca==a[j].Ca || a[i].Ca==a[j].Cb || a[i].Cb==a[j].Ca || a[i].Cb==a[j].Cb))
					br++;
			}
			break;
	}
	return br;
}

int main()
{
	int n;
	cin>>n;
	grana G[n-1];
	for(int i=0;i<n-1;i++)
		G[i].unos();
	vector<int> S=indexS(G,n-1);
	int br=0;
	for(int i=0;i<S.size();i++)
	{
		br=br+SIO(G,n-1,'S',S[i]);
	}
	cout<<br;
}