class Calc0123 def initialize(args) args = args.map { |l| l.chomp.split(/\s+/) } @n, @m = args.shift.first.to_i @as = args.shift.map(&:to_i) end def run cards = (1..@n).to_a @as.each do |a| c = cards[a - 1] case a when 1 # nop when @n cards = [c] + cards[0..-2] else cards = [c] + cards[0..(a - 2)] + cards[a..-1] end end cards.first end end puts Calc0123.new(STDIN.readlines).run if __FILE__ == $0