N = int(input()) M = int(input()) PQ = [list(map(int, input().split())) for _ in range(M)] for p, q in PQ: if N==p: N=q elif N==q: N=p print(N)