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

#GL0000272. Ngai vàng (Throne)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Ngai vàng (Throne)

Throne

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

There are NN chairs arranged in a circle, including one special chair called the throne. Takahashi initially sits SS chairs clockwise from the throne.

During each move, he travels exactly KK chair positions clockwise and sits on the resulting chair. Passing the throne during a move does not count; he must finish the move on the throne.

Find the minimum number of moves needed to reach the throne, or output -1 if it is impossible.

Input

One line contains three integers N,S,KN,S,K.

Output

Print the minimum number of moves, or -1 if the throne cannot be reached.

Subtasks

  • Subtask 1 (20 points): 2≤N≤1002\le N\le 100; 1≤S<N1\le S<N; 1≤K≤1001\le K\le 100.
  • Subtask 2 (30 points): 2≤N≤1000002\le N\le 100000; 1≤S<N1\le S<N; 1≤K≤1000001\le K\le 100000.
  • Subtask 3 (50 points): 2≤N≤10000000002\le N\le 1000000000; 1≤S<N1\le S<N; 1≤K≤10000000001\le K\le 1000000000.

Examples

Example 1

Input

10 4 3

Output

2

Explanation

Starting at position 44, two jumps of size 33 reach positions 77 and 00.

Example 2

Input

1000 11 2

Output

-1

Explanation

The starting position is odd and each jump is even, so the throne at position 00 cannot be reached.

Example 3

Input

998244353 897581057 595591169

Output

249561088

Explanation

The reported number is the smallest nonnegative solution of the congruence.

Example 4

Input

10000 6 14

Output

3571

Explanation

The sum 6+14⋅3571=500006+14\cdot3571=50000 is divisible by 1000010000, and this is the smallest valid move count.