//#pragma GCC optimize(2) //#pragma GCC optimize(3,"Ofast","inline") #include<bits/stdc++.h> #define int long long #define fi first #define se second #define pb push_back #define pii pair<int,int> #define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); using namespace std; const int inf=(int)8e18+100; const int maxn=2e5+100; int bsgs(int a,int b,int p,int ad) { map<int,int>mp; int m=ceill(sqrt(p)); int tp=1; mp[b%p]=0; for(int i=1;i<=m;i++) { tp=tp*a%p; mp[tp*b%p]=i; } int res=tp; for(int i=1;i<=m;i++) { if(mp.find(ad*res%p)!=mp.end()) { return i*m-mp[ad*res%p]; } res=res*tp%p; } return -1; } int exbsgs(int a,int b,int p) { a%=p; b%=p; if(b==1||p==1) return 0; int d=__gcd(a,p); int k=0; int ad=1; while(d!=1) { if(b%d!=0)return -1; k++; b/=d; p/=d; ad=ad*a/d%p; d=__gcd(a,p); if(ad==b)return k; } int ans=bsgs(a,b,p,ad); if(ans==-1)return ans; return ans+k; } signed main() { IOS int a,b,p; while(cin>>a>>p>>b) { if(a==0&&b==0&&p==0)break; int ans=exbsgs(a,b,p); if(ans==-1)cout<<"No Solution\n"; else cout<<ans<<"\n"; } }