#include <bits/stdc++.h>
using namespace std;
#define fo(a,b) for(int64_t a=0;a<b;a++)
#define sor(a) sort(a.begin(),a.end())
#define rev(a) reverse(a.begin(),a.end())
#define ll int64_t
#define mod 1000000007
#define vl vector<int64_t>
#define vc vector<char>
#define vs vector<string>
#define np(vec) next_permutation(vec.begin(),vec.end())==true
#define en endl
#define vvl vector<vector<int64_t>>
#define sti stack<int64_t>
#define qui que<int64_t>
#define ms(a,b) memset(a,b,sizeof(a))
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second

int main(){
  ll li;
  string x;
  cin>>x;
  ll s=x.size();
  vl vec(s);
  ll m,ma;
  ma=0;
  cin>>m;
  fo(i,s){
    vec.at(i)=x.at(i)-'0';
  }
  fo(i,s){
    ma=max(ma,vec.at(i));
  }
  if(s==1){if(vec.at(0)<=m){cout<<1<<endl;}
          else{cout<<0<<en;}}
  else{ll lb=-1; 
  ll ub=2*pow(10,18);
  while(ub-lb>1){
    ll mid=(lb+ub)/2;
    if(vec.at(0)*pow(mid,(s-1))>pow(10,18)){
      ub=mid;
    }
    else{lb=mid;}        
  }
  li=lb;
  ub=lb;
  lb=ma+1;
  if(lb>ub){cout<<0<<endl;}
  else{
    lb=0;
    ub=2*pow(10,18);
    while(ub-lb>1){
         ll mid=(lb+ub)/2;
      if(mid>li){ub=mid;}
         else{ll ans=0;
         fo(i,s){
           ans=ans*mid;
           ans+=vec.at(i);
           }
         if(ans>m){
           ub=mid;
         }
         else{lb=mid;}        
       }}
     cout<<max(lb-ma,0L)<<en;
  }    
      }     
}
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define pb push_back

bool check(ll a, ll b, ll m)
{
    if (a == 0 || b == 0)
        return false;
    if (a > m / b)
        return true;
    return false;
}

bool pos(vector<ll> &dig, ll x, ll m)
{
    ll num = dig[0], cur = 1;
    for (int i = 1; i < dig.size(); i++)
    {
        if (check(cur, x, m))
            return false;
        cur *= x;
        if (check(cur, dig[i], m))
            return false;
        if (num > m - (cur * dig[i]))
            return false;
        num += cur * dig[i];
        if (num > m)
            return false;
    }
    return num <= m;
}

ll solve()
{
    string str;
    ll m, res = 0;
    cin >> str >> m;
    ll d = 0;
    vector<ll> dig;
    for (char s : str)
    {
        dig.pb((s - '0') * 1ll);
        d = max(d, dig.back());
    }
    reverse(dig.begin(), dig.end());
    if (dig.size() == 1)
    {
        if (dig[0] > m)
            return 0;
        return 1;
    }
    ll l = d + 1, r = 1e18;
    while (l < r)
    {
        ll mid = l + (r - l) / 2;
        if (pos(dig, mid, m))
        {
            res = max(res, mid - d);
            l = mid + 1;
        }
        else
            r = mid - 1;
    }
    if (pos(dig, l, m))
        res = max(res, l - d);
    return res;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout << solve() << "\n";
    return 0;
}