#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define m_p make_pair
#define str string
#define lowbit(x) (x&(-x))
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pi;
//typedef __int128 it;
const int Maxn=1e6+10;
const int inf=0x3f;
ll a,n;
ll dis[Maxn];
ll sum,mx;
inline ll qpow(ll a,ll b)
{
ll ans=1;
while(b){
if(b&1) ans=(ans*a);
b>>=1;
a=(a*a);
}
return ans;
}
inline ll chnge(ll k)
{
ll tot=0;
ll kk=k;
while(kk){
tot++;
kk/=10;
}
// printf("%lld ",tot);
ll res=(k-k%10)/10+(k%10)*qpow(10,tot-1);
return res;
}
inline void dfs(ll k,ll dist)
{
// printf("%lld ",k);
if(k>mx || dis[k]!=-1) return ;
dis[k]=dist;
if(k==n) return ;
ll nxt1=k*a;
dfs(nxt1,dist+1);
if(k>=10 && k%10){
ll nxt2=chnge(k);
dfs(nxt2,dist+1);
}
}
int main()
{
scanf("%lld%lld",&a,&n);
ll nn=n;
while(nn){
sum++;
nn/=10;
}
mx=qpow(10,sum)-1;
// printf("%lld ",mx);
for(int i=1;i<Maxn;i++) dis[i]=-1;
dfs(1,0);
printf("%lld",dis[n]);
return 0;
}
WA在第11个点了qwq