N=int(raw_input()) M=int(raw_input()) for a in range(M): P, Q=map(int,raw_input().split()) if P==N: N=Q elif Q==N: N=P print N