class Calc0026 def initialize args = STDIN.readlines.map { |l| l.chomp.split(/\s+/) } @n = args.shift.first.to_i @m = args.shift.first.to_i @shuffles = args.map { |l| l.map(&:to_i) } end def calc n = @n @shuffles.each do |shuffle| if shuffle.include?(n) n = (shuffle - [n]).first end end n end def run puts calc end end Calc0026.new.run if __FILE__ == $0