#include<bits/stdc++.h>
using namespace std;
#define ll __int128
#define pll pair<ll,ll>
#define mp make_pair
#define fi first
#define se second
#define db double
#define pb push_back
#define N 1000000000
#define inf 10000000000000

char s[65];
ll len,mx,m;

inline ll ksm(ll a,ll b){ ll res=1;
    for(;b;b>>=1,a=a*a){
        if(b&1)res=res*a;
        if(res>m)return -1; 
    }
    return res;
}

inline ll read(){
    ll f=0,s=0; char c=getchar();
    while(c>'9'||c<'0')f=(c=='-'),c=getchar();
    while(c>='0'&&c<='9')s=(s<<3)+(s<<1)+(c^'0'),c=getchar();
    return f?-s:s;
}
inline void write(ll x)
{
    if(x<0)
    {
        putchar('-');
        x=-x;
    }
    if(x>9)
        write(x/10);
    putchar(x%10+'0');
}
inline ll check(ll x){ ll res=0;
    for(int i=len;i>=1;i--){
        ll tt=ksm(x,len-i);
        if(tt!=-1)res+=(s[i]-'0')*tt; //write(res); cout<<endl;
        else return 1e30;
    }
    return res;
}

int main(){
    scanf("%s",s+1); len=strlen(s+1),mx=0; m=read();
    for(int i=1;i<=len;i++)mx=max(mx,(ll)s[i]-'0');
    if(len==1){
        if(s[1]-'0'<=m)puts("1");
        else puts("0");
        return 0;
    }
    ll l=mx+1,r=m,mid,res=mx;
    while(l<=r){
        mid=(l+r)>>1; //write(l); putchar(' '); write(r); putchar(' '); write(mid); cout<<endl;
        if(check(mid)<=m)l=mid+1,res=mid;
        else r=mid-1;
    }
    write(res-mx);
    return 0;
}
#include <bits/stdc++.h>
using namespace std;

template<typename T>
struct counting_iterator: public iterator<random_access_iterator_tag, bool>{
	T value = 0;
	counting_iterator(const T &value): value(value){}
	counting_iterator(const counting_iterator &it): value(it.value){}
	counting_iterator(){}
	typename iterator_traits<counting_iterator>::difference_type operator-(const counting_iterator &it) const{ return value - it.value; }
	counting_iterator &operator++(){ return *this += 1; }
	counting_iterator &operator--(){ return *this += -1; }
	counting_iterator &operator+=(typename iterator_traits<counting_iterator>::difference_type n){ value += n; return *this; }
	bool operator!=(const counting_iterator &it) const{ return value != it.value; }
	T &operator*(){ return value; }
};
template<typename T>
using cnt_it = counting_iterator<T>;

int main(){
	cin.tie(0)->sync_with_stdio(0);
	cin.exceptions(ios::badbit | ios::failbit);
	string x;
	long long m;
	cin >> x >> m;
	if((int)x.size() == 1){
		cout << (x[0] - '0' <= m) << "\n";
		return 0;
	}
	int d = *max_element(x.begin(), x.end()) - '0';
	auto pred = [&](long long n){
		long long cur = 0;
		for(auto digit: x){
			if(__builtin_mul_overflow_p(cur, n, 0LL) || __builtin_add_overflow_p(cur * n, digit - '0', 0LL)){
				return false;
			}
			cur = cur * n + digit - '0';
		}
		return cur <= m;
	};
	cout << *partition_point(cnt_it<long long>(d + 1), cnt_it<long long>(numeric_limits<long long>::max()), pred) - d - 1 << "\n";
	return 0;
}

/*

*/

////////////////////////////////////////////////////////////////////////////////////////
//                                                                                    //
//                                   Coded by Aeren                                   //
//                                                                                    //
////////////////////////////////////////////////////////////////////////////////////////