/*
problem : 程序设计:寻找重复项
finished time : 2021/11/27 23:00 made by Aze
source : https://vjudge.ppsucxtt.cn/problem/%E8%AE%A1%E8%92%9C%E5%AE%A2-43120
https://nanti.jisuanke.com/t/43120
analysis :
this problem is to find first reappeared num
all sums calculated by expression (a * x + x % b) %c
so, we need a data abstraction which fill with them
and every data element bind a bool to show is it appeared
when we meet first reappear data, loop break, print now cnt
else cnt go beyond 2e6, print -1
*/
#include <iostream>
#include <unordered_map>
using namespace std;
unordered_map<int, bool> alls;
long long a, b, c, x = 1, y, cnt;
int main()
{
cin >> a >> b >> c;
do
{
alls[x] = true;
y = (a * x + x % b) % c;
x = y;
cnt++;
} while (!alls[y] && cnt <= 2e6);
if (cnt > 2e6) cout << -1 << endl;
else cout << cnt << endl;
return 0;
}