#include <bits/stdc++.h>
using namespace std;
int main(){
  string S;
  int64_t M,z=0;
  cin>>S>>M;
  int64_t N=S.size(),K=*max_element(S.begin(),S.end())-'0';
  if(N<2){
    cout<<(M<K?0:1);
    return 0;
  }
  int64_t ok=0,ng=M+10;
  while(ng-ok>1){
    __int128 m=(ok+ng)/2,sum=0,temp=1,l=0;
    for(int j=N-1;j>=0;temp*=m,j--){
      sum+=(S[j]-'0')*temp;
      if(temp>M){
        l=1;
        break;
      }
    }
    if(l||sum>M)ng=m;
    else ok=m;
  }
  cout<<max(z,ok-K);
}
#pragma GCC optimize ("O2")
#pragma GCC target ("avx")
//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
//#include<atcoder/all>
//using namespace atcoder;

using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

char cn[1200010];
char* ci = cn, * di = cn, ct;
char tc[200002], *d1 = tc + 100000, *d2 = tc + 200000;
const char d[2] = "0";

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	rep(i, 100000) tc[i] = '0';
	rep(i, 100000) d1[i] = '1';
	d2[0] = '0';
	d2[1] = '\n';
	fread(cn, 1, 1200010, stdin);
	int T = 0;
	while ((ct = *ci++) >= '0') T = T * 10 + ct - '0';

	rep(t, T) {
		int N = 0;
		while ((ct = *ci++) >= '0') N = N * 10 + ct - '0';
		memcpy(di, d1 - N, N);
		di += N;
		memcpy(di, d2 - N, N + 2);
		di += N + 2;
		ci += 6 * N + 3;
	}
	fwrite(cn, 1, di - cn, stdout);

	Would you please return 0;
}