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))