cap="123" idm=int(input()) idm-=1#match list index n=int(input()) for i in range(n): c1,c2=input().split() cap=cap.replace(c1,c2) print(cap[idm])