Under construction

Still being built. Things may change

Catapult-Carousel

Time limit1sMemory limit1024 MB

Summary
Given N seats and S affine jumps, find where seat U ends up after K rides, where K can be negative.
Level

Medium6 of 10

Topics
Math, Number theory
Solved
No attempts yet

Problem

Catapult-Carousel is a new experimental fairground attraction that John invented. It is a rotating wheel with NN seats spaced at equal distances. The seats are numbered 1…N1 \ldots N in clockwise order. Each seat has a mechanism that launches the rider into the air in a controlled manner, so that the rider lands on a predetermined seat.

An (X,Y)(X, Y)-jump is an operating mode of the carousel. In this mode, all riders are launched simultaneously. The rider launched from seat 11 lands on seat XX, and each subsequent rider (from seats 2…N2 \ldots N) lands YY positions clockwise from the previous landing position. John designed the carousel so that no two riders land on the same seat in any allowed jump mode.

One ride consists of several jumps. The number and parameters of the jumps are the same in every ride. Riders do not change seats between jumps, even when they take several rides in a row. John sells multiple-ride tickets that specify the initial seat and the number of rides. He wants to check that a rider is in the expected seat after completing all the rides.

Write a program that takes the seat number UU of a rider and computes their seat after KK rides. If KK is negative, the program computes the seat they were on ∣K∣|K| rides ago.

Input

The first line contains NN, the number of seats (2≤N<2632 \le N < 2^{63}), and SS, the number of jumps in a ride (1≤S≤1061 \le S \le 10^6).

Each of the following SS lines contains the parameters XiX_i and YiY_i of one jump (0<Xi≤N0 < X_i \le N, 0<Yi<N0 < Y_i < N, 1≤i≤S1 \le i \le S).

The last line contains UU, the seat number of the rider (1≤U≤N1 \le U \le N), and KK, the number of rides (−263≤K<263-2^{63} \le K < 2^{63}).

Output

The only line contains one integer: the seat the rider is on after KK rides. If KK is negative, print the seat the rider was on ∣K∣|K| rides ago.

Examples2

  1. Example 1

    Input
    11 2
    3 2
    5 7
    4 2
    
    Expected output
    1
    
  2. Example 2

    Input
    911 4
    3 2
    5 7
    133 22
    11 12
    1 -1
    
    Expected output
    825