N=int(input()) M=int(input()) P=[[int (i) for i in input().split()] for i in range(M)] #print (P) for i in range (M): if P[i][0]==N: N=P[i][1] elif P [i][1]==N: N=P[i][0] print (N)