#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll p;
ll ksc(ll x,ll y,ll p){//计算x乘y的积
ll res=0;//加法初始化
while(y){
if(y&1)res=(res+x)%p;//模仿二进制
x=(x<<1)%p; y>>=1;//将x不断乘2达到二进制
}return res;
}
ll pw(ll t,ll k){//二进制快速幂板子
ll res=1;
while(k>0)
{
if(k&1)res=(res*t)%p;
t=(t*t)%p;
k>>=1;
}
return res;
}
map<ll,ll>h;
map<ll,bool>vis;
ll BSGS(ll y,ll z){
h.clear();
ll t=ceil(sqrt(p));
y%p,z%=p;
ll w=z;
for(int b=0;b<=t;b++){
h[w]=b;
w=ksc(w,y,p)%p;
}
ll e=pw(y,t);
w=1;
for(int a=1;a<=t+1;a++){
w=ksc(w,e,p);
if(h[w]){
int b=h[w];
if(b>=0&&a*t-b>=0){
return a*t-b;
}
}
}
}
int main(){
ll k;
scanf("%lld%lld",&k,&p);
k=(9*k%p+1)%p;
ll ans=BSGS(10,k);
printf("%lld",ans);
// }
}
最后两个点wa了