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