#!/bin/python3 import sys import re route = [("L", "R").index(x) for x in input().strip()] map = {} for line in sys.stdin: line = line.strip() if len(line) == 0: continue line = re.sub("[^A-Z]+", " ", line) location, left, right = line.split() map[location] = (left, right,) START = "AAA" DESTINATION = "ZZZ" steps = 0 current_location = START while current_location != DESTINATION: route_idx = steps % len(route) steps += 1 current_location = map[current_location][route[route_idx]] print(f">> {steps}")