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

typedef long long int ll;
#define All(a) (a).begin(),(a).end()
#define INF numeric_limits<ll>::max()
#define MOD 1000000007
ll n;
vector<ll> v;

bool check(ll md){
	vector<ll> y;
	ll N=n;
	while(N>0){
		y.push_back(N%md);
		N/=md;
	}
	reverse(All(y));
	if(v.size()>y.size()) return false;
	if(v.size()<y.size()) return true;
	for(ll i=0;i<v.size();i++){
		if(v[i]<y[i])return true;
		if(v[i]>y[i])return false;
	}
	return true;	
}

int main(){
	ll ans,MAX=0,tmp;
	string s;	
	cin >> s>>n;
	
	if(s.size()==1){
		if((s[0]-'0')<=n){
			cout<<1<<endl;
		}else{
			cout<<0<<endl;
		}
		return 0;
	}

	for(auto i:s){
		tmp=i-'0';
		v.push_back(tmp);
		MAX=max(MAX,tmp);	
	}

	//reverse(All(v));
	
	ll ng=n+1,ma=MAX,md;
	//cout << ng <<" "<<ma<< endl;

	while(ng-ma>1){
		ll md=(ma+ng)/2;
		//cout<<md<<endl;
		if(check(md)){
			ma=md;
		}else{
			ng=md;
		}
			
	}	
	cout<<ma-MAX<<endl;
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a, b;
    cin >> a >> b;
    int ans = 1;
    for(int i = b - a; i ; i--)
    {
        if(b / i != (a + i - 1) / i)
        {
            cout << i;
            break;
        }
    }
}
