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