Rt,调了一晚上实在不知道哪里错了,求助qwq
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<unordered_map>
#include<set>
#include<deque>
#include<bitset>
#include<utility>
#include<string>
#include<cmath>
#include<ctime>
#include<cassert>
#include<cstdlib>
#include<random>
using namespace std;
#define ll long long
#define ull unsigned long long
#define db double
#define Int __int128
#define endl '\n'
#define y1 Y1
#define ls p<<1
#define rs p<<1|1
#define pii pair<int,int>
#define PII pair<ll,ll>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define F(i,x,y) for(int i=x;i<=y;++i)
#define Fo(i,x,y) for(int i=x;i>=y;--i)
#define mem(a,x) memset(a,x,sizeof(a))
#define bug cerr<<__LINE__<<' '<<__FUNCTION__<<endl
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
mt19937 seed(time(0));
template<class T>T rand(T l,T r){return uniform_int_distribution<int>(l,r)(seed);}
template<class T>T abss(T x){if(x<0) return -x;return x;}
template<class T>T Max(T x,T y){if(x>y) return x;return y;}
template<class T>T Min(T x,T y){if(x<y) return x;return y;}
template<class T>T lowbit(T x){return x&(-x);}
#define INF 0x3f3f3f3f
#define INFF 0x3f3f3f3f3f3f3f3f
#define eps 1e-8
#define mod 1000000007
#define N 200005
ll m,k;
ll ksm(ll a,ll b,ll p){
a%=p;
ll res=1;
while(b>0){
if(b&1) res=res*a%p;
a=a*a%p,b>>=1;
}return res;
}
ll bsgs(ll a,ll b,ll p){
unordered_map<ll,ll> m;
m.clear();
b%=p;
ll t=sqrt(p)+1;
for(int i=0;i<t;++i) m[b*ksm(a,i,p)%p]=i;
a=ksm(a,t,p);
if(a==0) return b==0?1:-1;
for(int i=1;i<=t;++i){
ll val=ksm(a,i,p);
int j=m.find(val)==m.end()?-1:m[val];
if(j>=0&&i*t-j>=0) return i*t-j;
}
return -1;
}
ll gcd(ll a,ll b){
if(b==0) return a;
return gcd(b,a%b);
}
ll exgcd(ll a,ll b,ll &x,ll &y){
if(b==0){
x=1,y=0;
return a;
}
ll g=exgcd(b,a%b,x,y);
y-=a/b*x;
return g;
}
ll inv(ll a,ll b){
ll x,y;
exgcd(a,b,x,y);
return (x%b+b)%b;
}
ll exbsgs(ll a,ll b,ll p){
a%=p,b%=p;
if(b==1||p==1) return 0;
ll g,cnt=0,res=1;
while((g=gcd(a,p))^1){
if(res==b) return cnt;
if(b%g){
if(b==1) return cnt;
return -1;
}
cnt++,b/=g,p/=g;
res=res*(a/g)%p;
}
// cerr<<a<<" "<<b<<" "<<p<<" "<<res<<" "<<cnt<<endl;
ll ans=bsgs(a,b*inv(res,p)%p,p);
if(ans==-1) return -1;
return ans+cnt;
}
int main(){IOS;
ll a,b,p;
cin>>a>>p>>b;
while(a+b+p!=0){
ll ans=exbsgs(a,b,p);
if(ans==-1) cout<<"No Solution"<<endl;
else cout<<ans<<endl;
cin>>a>>p>>b;
}
return 0;
}