Under construction

Still being built. Things may change

Game of Throwns

Time limit2sMemory limit512 MB

Summary
Track a token around a circle of children through clockwise or counterclockwise throws, undoing the last m throws on command.
Level

Easy3 of 10

Topics
Stack, Simulation, Array
Solved
No attempts yet

Problem

Daenerys keeps inventing games for the second grade computer science class she teaches. This week the children stand in a circle and (carefully) throw a petrified dragon egg around.

The nn children are numbered 00 to n−1n - 1 clockwise around the circle. Child 00 always starts with the egg. Daenerys calls out one of two things.

  1. A number tt, meaning the egg is thrown to the child tt positions clockwise from the current holder, wrapping around the circle if necessary. If tt is negative, the throw goes counterclockwise.
  2. The phrase undo m, meaning the last mm throws are undone. An undo command never undoes another undo command. It only undoes throws of the first kind.

For example, with 55 children and the four commands 8 -2 3 undo 2, the egg goes from child 00 to child 33, then from child 33 to child 11, then from child 11 to child 44. The undo 2 then sends the egg back from child 44 to child 11, and back again from child 11 to child 33. If Daenerys calls out 00, or any multiple of nn such as nn, −n-n, 2n2n or −2n-2n, the child holding the egg throws it straight up and (carefully) catches it again.

Write a program that determines where the egg ends up once every command has been carried out correctly. Do not ask what happens to the children if it is not.

Input

The first line contains the number of children nn and the number of commands Daenerys calls out kk. (1≤n≤301 \le n \le 30, 1≤k≤1001 \le k \le 100)

The second line contains the kk commands in order. Each command is either an integer pp (−10 000≤p≤10 000-10\,000 \le p \le 10\,000) giving how many positions to throw the egg clockwise, or undo m (m≥1m \ge 1) asking for the last mm throws to be undone. An undo m counts as one command, so the two kinds together number kk. Daenerys never has the children undo past the start of the game.

Output

Print the number of the child holding the egg at the end of the game.

Examples2

  1. Example 1

    Input
    5 4
    8 -2 3 undo 2
    
    Expected output
    3
    
  2. Example 2

    Input
    5 10
    7 -3 undo 1 4 3 -9 5 undo 2 undo 1 6
    
    Expected output
    2