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