using System; using System.Collections; using System.Collections.Generic; class TEST{ static void Main(){ Sol mySol =new Sol(); mySol.Solve(); } } class Sol{ public void Solve(){ Dictionary> D=new Dictionary>(); for(int i=0;i()); D[info[i][2]].Add(new Pair(info[i][0],info[i][1])); } HashSet[] H=new HashSet[2]; H[0]=new HashSet(); foreach(Pair p in D[route[0]]){ H[0].Add(p.to); } int now=0; int next=0; for(int i=0;i(); if(i==0){ foreach(Pair p in D[route[i]]){ H[next].Add(p.to); H[next].Add(p.from); } continue; } foreach(Pair p in D[route[i]]){ if(H[now].Contains(p.from))H[next].Add(p.to); if(H[now].Contains(p.to))H[next].Add(p.from); } } List L=new List(H[next]); L.Sort(); Console.WriteLine(L.Count); for(int i=0;iint.Parse(e));} static long[] rla(){return Array.ConvertAll(Console.ReadLine().Split(' '),e=>long.Parse(e));} static double[] rda(){return Array.ConvertAll(Console.ReadLine().Split(' '),e=>double.Parse(e));} }