Hilbert's Hash Browns

Count how many residues mod n are reachable as x^p + q mod n over all nonnegative integers x.

Hard8Number theoryMathCombinatoricsImplementationNo attempts yetTime limit1sMemory limit512 MB

Problem

Hilbert's Hotel has infinitely many rooms, numbered 0, 1, 2, and so on, so it takes in one more guest even when it looks full: every guest in room ii moves to room i+1i+1, and room 0 becomes free. The restaurant attached to the hotel, Hilbert's Hash Browns, is not infinite. It has a very large number of tables, but that number is fixed.

The waiter there is very lazy. Instead of keeping a record of which tables are free, he seats every customer with one simple formula. He asks the customer for the hotel room number xx, raises that number to the power pp, and adds qq. The result is huge and there are only nn tables, so he takes the remainder modulo nn and points the customer to that table. Tables are numbered 0 to n1n-1, and the customer goes to table (xp+q)modn(x^p + q) \bmod n. If someone already sits there, the customer leaves hungry.

The waiter picks new values of pp and qq every day, and he has noticed that on some days a table is never used however many customers arrive. With n=3n = 3, p=2p = 2 and q=1q = 1, table 0 is never used, because no integer xx satisfies x2+10(mod3)x^2 + 1 \equiv 0 \pmod 3.

Room numbers run over every nonnegative integer. Given pp, qq and nn, find the largest number of tables that can be used.

Input

The first line contains three integers pp, qq and nn separated by spaces (1p<2311 \le p < 2^{31}, 0q<2310 \le q < 2^{31}, 2n<2312 \le n < 2^{31}).

Output

Print the largest number of tables that can be used.