a = Array.new(4) a[gets.to_i] = true gets while line = gets do p, q = line.split.map(&:to_i) a[p], a[q] = a[q], a[p] end puts a.index{|i| i}