PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GL0000295. Đánh dấu ô (Marking)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đánh dấu ô (Marking)

Marking

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

There are NN squares numbered from 00 to N−1N-1, initially unmarked. All squares are marked one by one using the following procedure.

  • First, mark square 00.
  • Let AA be the most recently marked square. Start with x=(A+D) mod Nx=(A+D)\bmod N. While square xx is already marked, replace xx with (x+1) mod N(x+1)\bmod N. Mark the first unmarked square encountered and repeat.

The process ends after exactly NN marks. Find the index of the square marked in the KK-th operation, including the initial mark of square 00.

Input

One line containing three integers N,D,KN,D,K.

Output

Print the index of the square marked in the KK-th operation.

Subtasks

  • Subtask 1 (20 points): 1≤K≤N≤1001\le K\le N\le 100; 1≤D≤1001\le D\le 100.
  • Subtask 2 (30 points): 1≤K≤N≤1000001\le K\le N\le 100000; 1≤D≤1000001\le D\le 100000.
  • Subtask 3 (50 points): 1≤K≤N≤10000000001\le K\le N\le 1000000000; 1≤D≤10000000001\le D\le 1000000000.

Examples

Examples 1

Input

4 2 3

Output

1

Explanation

The marking order is 0,2,1,30,2,1,3. On the third operation, square 00 is marked already, so the next unmarked square is 11.

Examples 2

Input

5 8 5

Output

2

Explanation

With step size 88, the marking order is 0,3,1,4,20,3,1,4,2, so the fifth square is 22.