#include <bits/stdc++.h>
#define pb push_back
typedef unsigned long long ll;
using namespace std;
double lg;
ll m;
ll calculate(string x, ll d) {
	ll sum = 0;
	ll temp = 1;
	for (int i = x.size() - 1; i >= 0; --i) {
		ll c = x[i] - '0';
		if (log2(c) + log2(temp) > lg)
			return 0;
		sum += c * temp;
		if (sum > m)
			return 0;
		if (log2(temp) + log2(d) > lg and i)
			return 0;
		temp *= d;
	}
	return sum;
}
int main() {
	string x;
	cin >> x >> m;
	ll cnt = 0;
	ll d = 0;
	for (char c : x)
		d = max(c - '0' + 1ULL, d);
	bool f = true;
	lg = log2(m);
	ll l = d + 1, r = 1e18;
	ll ans = 1;
	if (x.size() == 1) {
		if (x[0] - '0' > m)
			ans = 0;
	}
	else if (x.size() < 4) {
		while (l <= r) {
			ll mid = (l + r) >> 1;
			if (calculate(x, mid) == 0 or calculate(x, mid) <= calculate(x, mid - 1))
				r = mid - 1;
			else {
				ans = mid - d + 1;
				l = mid + 1;
			}
		}
	}
	else {
		ll sum2 = 0;
		ans = 0;
		while (true) {
			ll sum = calculate(x, d);
			if (sum <= sum2)
				break;
			++d;
			++ans;
		}
	}
	cout << ans << endl;
}
#include <bits/stdc++.h>
#define ll long long
#define X first
#define Y second
#define sz(x) (int)((x).size())
#define all(x) x.begin(), x.end()
#define pii pair<ll, int>
#define pi pair<int, pair<int, int>>

using namespace std;

typedef vector<int> vi;
typedef vector<unsigned int> vui;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef vi vm;
typedef vector<vm> Mat;

const string yes = "YES", no = "NO";


int main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0), cout.precision(10), cout << fixed;


    string str; cin >> str;
    reverse(str.begin(), str.end());
    ll m; cin >> m;
    if (str.size() == 1) {
        if (str[0] - '0' <= m)
            cout << 1;
        else cout << 0;
        cout << '\n';
        return 0;
    }

    ll mx = *max_element(str.begin(), str.end()) - '0';

    auto isValid = [&](ll base) {
        ll sum = str[0] - '0', b = 1;
        for (int i = 1; i < (int)str.size(); ++i) {
            if (__builtin_mul_overflow(b, base, &b)) {
                return false;
            }
            ll toAdd;
            if (__builtin_mul_overflow(str[i] - '0', b, &toAdd)) {
                return false;
            }
            if (__builtin_add_overflow(sum, toAdd, &sum)) {
                return false;
            }
        }
        return sum <= m;
    };

    ll l = mx, r = 1e18 + 1.0l;
    while (true) {
        ll mid = (l + r) / 2;
        if (mid == l) break;
        if (isValid(mid)) l = mid;
        else r = mid;
    }

    cout << l - mx << '\n';

    return 0;
}