N = int(input()) M = int(input()) c = [0, 1, 2, 3] for _ in range(M): P, Q = map(int, input().split()) c[P], c[Q] = c[Q], c[P] print(c.index(N))