#include <bits/stdc++.h>
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
long long m;
int l;
string x;
bool check(long long d){
	long long tmp=0,base=1;
	for (int i=l-1;i>=0;i--){
		if (base>m) return false;
		tmp+=base*(x[i]-'0');
		if (tmp>m) return false;
		if (base>(m/d)&&i!=0) return false;
		base*=1ll*d;
	}
	//cout<<d<<" "<<tmp<<endl;
	return true;
}
int main(){
	IO;
	cin>>x;
	cin>>m;
	if (x.size()==1){
		if (x[0]-'0'>m) cout<<0<<endl;
		else cout<<1<<endl;
		return 0;
	}
	l=x.size();
	long long d=0;
	for (int i=0;i<l;i++){
		d=max(d,(long long)(x[i]-'0'));
	}
	d++;
	long long l=d,ans=0,r=1e18;
	while (l<=r){
		long long mid=(l+r)/2;
		if (check(mid)){
			ans=mid-d+1;
			l=mid+1;
		}else r=mid-1;
	}
	cout<<ans<<endl;
	return 0;
}


using namespace std;
#include <bits/stdc++.h>
int A,B;
int main(){
	scanf("%d%d",&A,&B);
	int sum=max(A,B)*(max(A,B)+1)/2;
	if (A<B){
		for (int i=1;i<=B;++i)
			printf("%d ",-i);
		for (int i=1;i<A;++i)
			printf("%d ",i),sum-=i;
		printf("%d\n",sum);
	}
	else{
		for (int i=1;i<=A;++i)
			printf("%d ",i);
		for (int i=1;i<B;++i)
			printf("%d ",-i),sum-=i;
		printf("%d\n",-sum);
	}
	return 0;
}