27 lines
439 B
Python
27 lines
439 B
Python
import sys
|
|
from itertools import cycle
|
|
|
|
L = sys.stdin.read().splitlines()
|
|
N = L[2:]
|
|
|
|
M = []
|
|
for x in N:
|
|
(a, b) = x.split(" = ")
|
|
c = b.split(", ")
|
|
M.append( (a, c[0].replace("(", ""), c[1].replace(")", "")) )
|
|
|
|
S = 0
|
|
tri = "AAA"
|
|
|
|
for i in cycle(L[0]) :
|
|
for a, b, c in M:
|
|
if a == tri:
|
|
#print(a, b, c)
|
|
tri = b if i == "L" else c
|
|
break
|
|
S+=1
|
|
if tri == "ZZZ" : break
|
|
|
|
print(S)
|
|
|