#include <iostream>
#include<algorithm>

using namespace std;

typedef unsigned long long ull;
string x;
ull m;

bool check(ull mid)
{
    ull ans=0;
    int flag=1;
    for(int i=0;i<x.size();i++)
    {
        if(ans > m / mid) return false;
        ans=ans*mid+(x[i]-'0');
    }
    return ans <= m;
}

int main()
{
    char c='0';
    cin>>x>>m;
    for(int i=0;i<x.size();i++)
    {
        c=max(c,x[i]);
    }
    ull t=c-'0';
    if(x.size()==1)
    {
        if(t-1<=m) cout<<1<<endl;
        else cout<<0<<endl;
    }
    else
    {
        ull l=0,r=1e18;
        while(l<r)
        {
            ull mid=(l+r+1)>>1;
            if(check(mid)) l=mid;
            else r=mid-1;
        }
        if(l<t) cout<<0<<endl;
        else cout<<l-t<<endl;
    }

    return 0;
}

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

#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define reps(i, s, n) for (int i = (s); i < (int)(n); ++i)
#define all(obj) begin(obj), end(obj)
#define allr(obj) rbegin(obj), rend(obj)
#define cinv(a) rep(i,(int)a.size()) cin >> a[i] 
#define debug(a) rep(i,(int)a.size()) cout << a[i] << " "
#define pb push_back
#define eb emplace_back
#define dame {puts("-1"); return 0;}
typedef long long ll;
typedef pair<int, int> Pi;
typedef vector<Pi> vp;
typedef vector<vp> vvp;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;

template<typename T>
bool chmax(T& a, const T& b){if(a < b){a=b;return true;}return false;}
template<typename T>
bool chmin(T& a, const T& b){if(a > b){a=b;return true;}return false;}

const int di[] = {0, -1, 0, 1};
const int dj[] = {1, 0, -1, 0};
const int MOD = 1e9+7;
const int INF = 1e9;
const char nl = '\n';

ll power(ll a, ll b) {return b? power(a*a%MOD, b/2)*(b%2?a:1)%MOD:1;}

ll C(int n, int k)
{
	ll x = 1, y = 1;
	for (int i = 1; i <= k; ++i)
	{
		x = x*(n-i+1)%MOD;
		y = y*i%MOD;
	}
	return x*power(y, MOD-2)%MOD;
}

struct UF
{
	vi d;
	UF(int n) : d(n, -1) {}
	int root(int x)
	{
		if (d[x] < 0) return x;
		return d[x] = root(d[x]);
	}
	bool unite(int x, int y)
	{
		x = root(x); y = root(y);
		if (x == y) return false;
		if (d[x] > d[y]) swap(x, y);
		d[x] += d[y];
		d[y] = x;
		return true;
	}
	bool same(int x, int y) { return root(x) == root(y); }
	int size(int x) { return -d[root(x)]; }
};

bool f(ll base, string x, ll m)
{
	ll sum = 0;
	rep(i, x.size())
	{
		if (log10(sum) + log10(base) >= 18.5) return false;
		sum *= base; 
		sum += x[i]-48;
	}
	return sum <= m;
}

int main() 
{
	string x; cin >> x;
	ll m ; cin >> m;
	if (x.size() == 1)
	{
		cout << (stoll(x) <= m) << endl;
		return 0;
	}
	int n = 0;
	rep(i, x.size()) chmax(n, x[i]-48);
	ll left = 1;
	ll right = m+1;
	while (right - left > 1)
	{
		ll mid = left + (right-left)/2;
		if (f(mid, x, m)) left = mid;
		else right = mid;
	}
	cout << max(0LL, left-n) << endl;
}

