#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read(){
   int s=0,w=1;
   char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
   return s*w;
}


const int maxn = 1000;
 
struct bign{
    int d[maxn], len;
 
	void clean() { while(len > 1 && !d[len-1]) len--; }
 
    bign() 			{ memset(d, 0, sizeof(d)); len = 1; }
    bign(int num) 	{ *this = num; } 
	bign(char* num) { *this = num; }
    bign operator = (const char* num){
        memset(d, 0, sizeof(d)); len = strlen(num);
        for(int i = 0; i < len; i++) d[i] = num[len-1-i] - '0';
        clean();
		return *this;
    }
    bign operator = (int num){
        char s[20]; sprintf(s, "%lld", num);
        *this = s;
		return *this;
    }
 
    bign operator + (const bign& b){
        bign c = *this; int i;
        for (i = 0; i < b.len; i++){
        	c.d[i] += b.d[i];
        	if (c.d[i] > 9) c.d[i]%=10, c.d[i+1]++;
		}
		while (c.d[i] > 9) c.d[i++]%=10, c.d[i]++;
		c.len = max(len, b.len);
		if (c.d[i] && c.len <= i) c.len = i+1;
        return c;
    }
    bign operator - (const bign& b){
        bign c = *this; int i;
        for (i = 0; i < b.len; i++){
        	c.d[i] -= b.d[i];
        	if (c.d[i] < 0) c.d[i]+=10, c.d[i+1]--;
		}
		while (c.d[i] < 0) c.d[i++]+=10, c.d[i]--;
		c.clean();
		return c;
    }
    bign operator * (const bign& b)const{
        int i, j; bign c; c.len = len + b.len; 
        for(j = 0; j < b.len; j++) for(i = 0; i < len; i++) 
			c.d[i+j] += d[i] * b.d[j];
        for(i = 0; i < c.len-1; i++)
            c.d[i+1] += c.d[i]/10, c.d[i] %= 10;
        c.clean();
		return c;
    }
	bign operator += (const bign& b){
        *this = *this + b;
        return *this;
    }
 
    bool operator <(const bign& b) const{
        if(len != b.len) return len < b.len;
        for(int i = len-1; i >= 0; i--)
            if(d[i] != b.d[i]) return d[i] < b.d[i];
        return false;
    }
    bool operator >(const bign& b) const{return b < *this;}
    bool operator<=(const bign& b) const{return !(b < *this);}
    bool operator>=(const bign& b) const{return !(*this < b);}
    bool operator!=(const bign& b) const{return b < *this || *this < b;}
    bool operator==(const bign& b) const{return !(b < *this) && !(b > *this);}
 
    string str() const{
        char s[maxn]={};
        for(int i = 0; i < len; i++) s[len-1-i] = d[i]+'0';
        return s;
    }
};
bign M;
char s[103];
bool check(int len,int base)
{
	int A=1,B=0;
	bign BASE=base,t=A,sum=B;
	for(int i=len; i>=1; i--)
	{
		sum=sum+(t*(s[i]-48));
		if(sum>M) return 0;
		if(i==1) return 1;
		t=t*BASE;
		if(t>M) return 0;
	}
	return 1;
}
signed main()
{
	scanf("%s",s+1);
	int m=read(),l=strlen(s+1),g=1;
	if(l==1)
	{
		if((int)s[1]-48>m) puts("0");
		else puts("1");
		return 0;
	}
	M=m;
	for(int i=1; i<=l; i++) g=max((int)s[i]-48,g);
	++g;
	int ll=g,rr=m,ans=g-1;
	while(ll<=rr)
	{
		int mid=(ll+rr)>>1;
		if(check(l,mid)) ans=mid,ll=mid+1;
		else rr=mid-1;
	}
	printf("%lld\n",ans-g+1);
	return 0;
}
#include <algorithm>
#include <cmath>
#include <vector>
#include <functional>
#include <cstdlib>
#include <map>
#include <set>
#include <iostream>
#include <string>
#include <ctype.h>
#include <climits>
#include <stack>
#include <queue>
#include <cassert>
#include <iomanip>
using namespace std;
typedef long long ll;
#define REP(i, n) for(ll i = 0; i < (ll)(n); ++i)
#define FOR(i, a, b) for(ll i=(a); i < (ll)(b); ++i)
template<class T> inline bool chmax(T& a, T b) { if(a < b){ a=b; return 1; } return 0;}
template<class T> inline bool chmin(T& a, T b) { if(a > b){ a=b; return 1; } return 0;}


bool func(string x, ll n, ll m){
	ll a=0;
	REP(i,x.size()){
		ll num = (ll)(x[i] - '0');
		
		if(a >= LLONG_MAX/n) return false;
		a = a*n;
		if(num >= LLONG_MAX/n) return false;
		if(a >= LLONG_MAX - num*n) return false;
		a += num;
		if(a > m) return false;
	}
	
	return true;
}

int main(){
	string x;
	ll m;
	cin >> x >> m;
	
	ll d=0;
	REP(i,x.size()){
		chmax(d, (ll)(x[i]-'0'));
	}
	
	if(x.size()==1){
		ll num = (ll)(x[0]-'0');
		if(num > m){
			cout << "0\n";
		}else{
			cout << "1\n";
		}
		return 0;
	}
	
	ll ok = d;
	ll ng = m+1;
	ll mid;
	
	while(ng - ok > 1){
		mid = (ok+ng)/2;
		
		if(func(x,mid,m)) ok = mid;
		else ng = mid;
	}
	
	cout << ok - d << endl;
	return 0;
}