c = [0]*4
c[input()] = 1
M = input()
for i in range(M):
	p,q = map(int,raw_input().split())
	c[p],c[q] = c[q],c[p]
print c.index(1)