n=int(input()) m=int(input()) l=[0,1,2,3] for i in range(m): s,t=map(int,input().split()) l[s],l[t]=l[t],l[s] print(l.index(n))