n=int(input()) m=int(input()) for i in range(m): a,b=map(int,input().split()) if a==n: n=b elif b==n: n=a print(n)