N = int(input()) M = int(input()) for i in range(M) : p,q = map(int,input().split()) if N == p : N = q elif N == q : N = p print(N)