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

namespace ConsoleApp5
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            int[,] a = new int[n, 2];
            string[] str1 = new string[n-1];
            for (int i=0; i<n-1; i++)
            {
                string[] s = Console.ReadLine().Split(' ');
                a[i, 0] = int.Parse(s[0]) - 1;
                a[i, 1] = int.Parse(s[1]) - 1;
                str1[i] = s[2];
            }
            string[] str = new string[n-1];
            for (int i=0; i<n-1; i++)
            {
                int x = Math.Min(a[i, 0], a[i, 1]);
                str[x] = str1[i];
            }
            int br = 0;
            for (int i = 0; i <n-3; i++)
            {
                int s = 0;
                int ii = 0;
                int o = 0;
                for (int j=i; j<n-3; j+=3)
                {
                    if (str[i] == "S")
                        s++;
                    if (str[i + 1] == "S")
                        s++;
                    if (str[i + 2] == "S")
                        s++;
                    if (str[i] == "I")
                        ii++;
                    if (str[i + 1] == "I")
                        ii++;
                    if (str[i + 2] == "I")
                        ii++;
                    if (str[i] == "O")
                        o++;
                    if (str[i + 1] == "O")
                        o++;
                    if (str[i + 2] == "O")
                        o++;
                    if (s == ii && s == o)
                        br++;
                }
            }
            Console.WriteLine(br);
        }
    }
}