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