Hanafuda Shuffle

Time limit1sMemory limit128 MB

Summary
Cards 1 to n start from the bottom, and each cut moves a contiguous block of c cards starting at position p to the top; report the top card after all cuts.
Level

Easy3 of 10

Topics
Simulation, Implementation, Array
Solved
No attempts yet

Problem

There are many ways to shuffle a deck of cards. The Hanafuda shuffle, used for the Japanese card game Hanafuda, is one of them. It works as follows.

You have a deck of nn cards. In one cutting operation, you take the cc cards that begin at the pp-th card from the top, pull that contiguous block out of the deck, and place it on top of the deck, keeping the block's internal order unchanged. A shuffle applies a sequence of such cutting operations, one after another.

Write a program that simulates a Hanafuda shuffle and reports which card ends up on top of the deck.

Input

The input consists of several data sets. Each data set begins with a line containing two positive integers nn and rr (1≤n≤501 \le n \le 50, 1≤r≤501 \le r \le 50): the number of cards in the deck and the number of cutting operations, respectively.

The next rr lines each describe one cutting operation, and the operations are performed in the given order. Each such line contains two positive integers pp and cc with p+c≤n+1p + c \le n + 1: starting from the pp-th card from the top, cc cards are pulled out and placed on top.

The end of the input is a line containing two zeros. Every input line contains exactly two integers separated by a single space, with no other characters.

Output

For each data set, print the number of the top card after the shuffle on its own line. At the start, the cards are numbered 11 to nn from the bottom of the deck to the top. Do not print any extra characters such as leading or trailing spaces.

Examples1

  1. Example 1

    Input
    5 2
    3 1
    3 1
    10 3
    1 10
    10 1
    8 3
    0 0
    
    Expected output
    4
    4