n=gets.to_i m=gets.to_i answer = n m.times do p, q=gets.split.map(&:to_i) if answer == p answer = q elsif answer == q answer = p end end puts answer