eolymp
bolt
Try our new interface for solving problems
Problems

Very simple

Very simple

Alice and Bob want to secretly transmit messages to each other, and for this they have developed a random number generator (RNG), which is initialized with three integers: a [0], a [1] and n. The first elements of the RNG are a0 and a1, the following elements are constructed like this: ai+2 = (ai+1 * ai+1 + ai * ai) mod n, i = 0, 1, ...

Alice and Bob will use the RNG in the data transfer scheme, as shown in the figure.

prb4898

To create a RNG, they want to write a procedure that calculates the value of ak for a given k. Help them!

Input

One line contains four integers n, a0, a1 and k, where 0ak, ak < n200, и 0k109.

Output

Print one number ak.

Time limit 1 second
Memory limit 128 MiB
Input example #1
10 0 1 4
Output example #1
5
Input example #2
10 2 4 5
Output example #2
2
Input example #3
200 133 166 233266300
Output example #3
146
Source 2005 Petrozavodsk, SPb ETU Contest, August 25, Problem G