#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');
  }
}
template<class S, class T> inline S chmax(S &a, T b){
  if(a<b){
    a=b;
  }
  return a;
}
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;
  }
  if(N==2){
    res = (M - X[1]) / X[0] - d;
    chmax(res, 0);
    wt_L(res);
    wt_L('\n');
    return 0;
  }
  long long FmcKpFmN;
  long long xr20shxY;
  long long WYIGIcGE;
  FmcKpFmN = d;
  xr20shxY = 1e9;
  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,1e9][
//     val = 0;
//     rep(i,N){
//       val = val * b + X[i];
//       if(val > M) break;
//     }
//   ](val <= M);
//   res = b - d;
//   wt(res);
// }

#include <iostream>
#include <cstdio>
#include <vector>
#include <iomanip>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
using namespace std;

string toBase(long long M, long long b) {
    string s;

    while (M) {
        long long maxVal = min(9LL, (M%b));
        if (M%b > 9) {
            for (int i = 0; i < s.size(); ++i) {
                s[i] = '9';
            }
        }
        s += (maxVal+'0');
        M /= b;
    }

    reverse(s.begin(), s.end());

    return s;
}

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

    long long minb = 0;
    long long maxb = M;
    // d*B^X <= M => logd + X log B <= log M => X log B <= logM - logd
    reverse(X.begin(), X.end());
    for (int i = 0; i < X.size(); ++i) {
        long long digit = X[i]-'0';
        minb = max(minb, digit+1LL);
    }

    if (X.size() == 1) { //only one digit
        maxb = minb;
    }

    long long low = minb;
    long long up = maxb;
    long long ans = minb-1;
    // cout << "DEBUG " << low << " " << maxb << endl;
    // cout << exp(log(1)) << endl;

    reverse(X.begin(), X.end());

    while (low <= up) {
        long long mid = (low+up) / 2;

        bool ok = true;

        string MS = toBase(M, mid);
        // cout << "DEBUGGO " << mid << " " << MS << endl;

        if (MS.size() < X.size() || (MS.size() == X.size() && MS < X)) {
            ok = false;
        }

        if (ok) {
            low = mid+1;
            ans = mid;
        } else {
            up = mid-1;
        }
    }

    cout << (ans-minb)+1 << endl;

    return 0;
}