using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        struct branch 
        {
            public int x;
            public int y;
            public char c;
        }

        static int solve(branch x,branch y, int j, string s, branch[] all, ref int rez) 
        {
            s += x.c;
            if (j == 2)
            {
                if (s == "SIO") 
                {
                    
                    rez++;
                }
                return 0;
            }
            j++;
            if (j == 1)
            {
                for (int i = 0; i < all.Length; i++)
                {
                    if ((x.x == all[i].x || x.x == all[i].y || x.y == all[i].x || x.y == all[i].y) && all[i].c == 'I') solve(all[i],x, j, s, all, ref rez);
                }
            }
            else 
            {
                for (int i = 0; i < all.Length; i++)
                {
                    if ((x.x == y.x || x.x == y.y) && all[i].c == 'O')
                    {
                        if (x.y == all[i].x || x.y == all[i].y) solve(all[i], x, j, s, all, ref rez);
                    }
                    else if (all[i].c == 'O') ;
                    {
                        if (x.x == all[i].x || x.x == all[i].y) solve(all[i], x, j, s, all, ref rez);
                    }
                }
            }
            return 0;
        }


        
        static void Main(string[] args)
        {
            //--------input

            int n = int.Parse(Console.ReadLine());
            string[] s;
            branch[] all = new branch[n - 1];
            branch[] start = new branch[n - 1];
            int k = 0;
            for(int i = 0; i < n - 1; i++)
            {
                s = Console.ReadLine().Split();
                all[i].x = int.Parse(s[0]);
                all[i].y = int.Parse(s[1]);
                all[i].c = char.Parse(s[2]);
                if (char.Parse(s[2]) == 'S') 
                {
                    start[k].x = int.Parse(s[0]);
                    start[k].y = int.Parse(s[1]);
                    start[k].c = char.Parse(s[2]);
                    k++;
                }
            }

            //----x---input
            int j = 0;
            int rez = 0;
            string s1 = "";
            for (int i = 0; i < k ;i++)
            {
                solve(start[i],start[i], j, s1, all, ref rez);
            }
            Console.WriteLine(rez);

            Console.ReadKey();

        
        
        }
    }
}