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