c = [false]*4 c[gets.to_i] = true m = gets.to_i m.times{ s,t = gets.split.map(&:to_i) c[s],c[t] = c[t],c[s] } p c.index(true)