intcount(intconst &n){ for (int i = h[n % MOD]; i; i = i[nxt]) if (i[id] == n) return1; return0; }
int &operator[](intconst &n) { for (int i = h[n % MOD]; i; i = i[nxt]) if (i[id] == n) return i[val]; ++numr, numr[id] = n, numr[nxt] = h[n % MOD], h[n % MOD] = numr; return numr[val]; } } mp;
intex_gcd(int a, int b, int &x, int &y){ if (!b) { x = 1, y = 0; return a; } int ret = ex_gcd(b, a % b, y, x); y -= a / b * x; return ret; }
intget_gcd(int a, int b){ int x, y; returnex_gcd(a, b, x, y); }
intget_inv(int a, int b){ int x, y; returnex_gcd(a, b, x, y), (x + b) % b; }
intbsgs(int a, int b, int c){ a %= c; if (a == 0) return b == 0 ? 1 : -1; if (b == 0) return-1; if (b == 1) return0; int k = 0; for (int t; (t = get_gcd(a, c)) > 1; ++k) { if (b % t) return-1; c /= t, b /= t, b = 1ll * b * get_inv(a / t, c) % c; } mp.clear(); int d = a, m = sqrt(c) + 1; for (int i = 1; i <= m; ++i, d = 1ll * d * a % c) mp[1ll * d * b % c] = i; d = 1ll * d * get_inv(a, c) % c; int e = d; for (int i = 1; i <= m; ++i, e = 1ll * e * d % c) if (mp.count(e)) return i * m - mp[e] + k; return-1; }
intmain(){ for (int a, b, c; scanf("%d%d%d", &a, &c, &b), a || b || c;) { int ans = bsgs(a, b, c); if (~ans) printf("%d\n", ans); else puts("No Solution"); } return0; }