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