#include <bits/stdc++.h>
using namespace std;

int main(){
    string X;
    long long M;

    cin >> X >> M;
    int d = (*max_element(X.begin(), X.end())) - '0';

    if(X.size() == 1){
        if(atoi(X.c_str()) <= M)cout << "1\n";
        else cout << "0\n";

        return 0;
    }

    auto ok = [&](long long base){
        long long power = 1, total = 0;
        for(int i = (int)X.size() - 1; i >= 0; i--){
            long long cur;
            if((X[i] - '0') > M / power)cur = M + 1;
            else cur = (X[i] - '0') * power;

            total = min(M + 1, total + cur);

            if(power > M / base)power = M + 1;
            else power = power * base;
        }

        if(total <= M)return true;
        else return false;
    };

    if(!ok(d + 1)){
        cout << "0\n";
        return 0;
    }

    long long L = d + 1, R = M;
    while(L < R){
        long long mid = (L + R + 1) / 2;
        if(ok(mid))L = mid;
        else R = mid - 1;
    }
    cout << L - d << '\n';

    return 0;
}
#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
template<class S, class T> inline S max_L(S a,T b){
  return a>=b?a:b;
}
inline int my_getchar_unlocked(){
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if(s == e && e == 1048576){
    e = fread_unlocked(buf, 1, 1048576, stdin);
    s = 0;
  }
  if(s == e){
    return EOF;
  }
  return buf[s++];
}
inline void rd(long long &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
inline void rd(char &c){
  int i;
  for(;;){
    i = my_getchar_unlocked();
    if(i!=' '&&i!='\n'&&i!='\r'&&i!='\t'&&i!=EOF){
      break;
    }
  }
  c = i;
}
inline int rd(char c[]){
  int i;
  int sz = 0;
  for(;;){
    i = my_getchar_unlocked();
    if(i!=' '&&i!='\n'&&i!='\r'&&i!='\t'&&i!=EOF){
      break;
    }
  }
  c[sz++] = i;
  for(;;){
    i = my_getchar_unlocked();
    if(i==' '||i=='\n'||i=='\r'||i=='\t'||i==EOF){
      break;
    }
    c[sz++] = i;
  }
  c[sz]='\0';
  return sz;
}
struct MY_WRITER{
  char buf[1048576];
  int s;
  int e;
  MY_WRITER(){
    s = 0;
    e = 1048576;
  }
  ~MY_WRITER(){
    if(s){
      fwrite_unlocked(buf, 1, s, stdout);
    }
  }
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a){
  if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
    fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
    MY_WRITER_VAR.s = 0;
  }
  MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
  my_putchar_unlocked(a);
}
inline void wt_L(long long x){
  int s=0;
  int m=0;
  char f[20];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar_unlocked('-');
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
int N;
char X[70];
long long M;
int main(){
  int i;
  long long res = 0;
  long long d;
  long long mx;
  long long b;
  __int128_t val;
  N = rd(X);
  rd(M);
  for(i=(0);i<(N);i++){
    X[i] -= '0';
  }
  int Q5VJL1cS;
  char e98WHCEY;
  if(N==0){
    e98WHCEY = 0;
  }
  else{
    e98WHCEY = X[0];
    for(Q5VJL1cS=(1);Q5VJL1cS<(N);Q5VJL1cS++){
      e98WHCEY = max_L(e98WHCEY, X[Q5VJL1cS]);
    }
  }
  d =e98WHCEY;
  if(N==1){
    if(X[0] <= M){
      res++;
    }
    wt_L(res);
    wt_L('\n');
    return 0;
  }
  long long FmcKpFmN;
  long long xr20shxY;
  long long WYIGIcGE;
  FmcKpFmN = d;
  xr20shxY = 1e18;
  while(FmcKpFmN < xr20shxY){
    if((FmcKpFmN + xr20shxY)%2==0){
      WYIGIcGE = (FmcKpFmN + xr20shxY) / 2;
    }
    else{
      WYIGIcGE = (FmcKpFmN + xr20shxY + 1) / 2;
    }
    val = 0;
    for(i=(0);i<(N);i++){
      val = val * WYIGIcGE + X[i];
      if(val > M){
        break;
      }
    }
    if(val <= M){
      FmcKpFmN = WYIGIcGE;
    }
    else{
      xr20shxY = WYIGIcGE - 1;
    }
  }
  b =xr20shxY;
  res = b - d;
  wt_L(res);
  wt_L('\n');
  return 0;
}
// cLay version 20210103-1 [bug fixed 4]

// --- original code ---
// int N; char X[70];
// ll M;
// {
//   ll res = 0, d, mx, b;
//   __int128_t val;
//   rd(X@N,M);
//   rep(i,N) X[i] -= '0';
//   d = max(X(N));
//   
//   if(N==1){
//     if(X[0] <= M) res++;
//     wt(res);
//     return 0;
//   }
//   // if(N==2){
//   //   res = (M - X[1]) / X[0] - d;
//   //   res >?= 0;
//   //   wt(res);
//   //   return 0;
//   // }
//   b = bsearch_max[ll,b,d,1e18][
//     val = 0;
//     rep(i,N){
//       val = val * b + X[i];
//       if(val > M) break;
//     }
//   ](val <= M);
//   res = b - d;
//   wt(res);
// }
