#include <iostream>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
typedef unsigned long long ll;

string X; ll M;
ll INF = 1ll << 61;

bool isBigger(ll base) {
	int n = X.size();
	if (log(X[0] - '0') + (n - 1) * log(base) > log(INF)) { return true; }

	ll c = 1;
	for (int i = 1; i < n && c <= M; i++) { c *= base; }
	if (c > M) { return true; }
	ll cur = 0;
	for (int i = 0; i < X.size(); i++) {
		cur += c * (X[i] - '0');
		c /= base;
		if (cur > M) { return true; }
	}
	return false;
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);

	cin >> X >> M;
	int d = *max_element(X.begin(), X.end()) - '0';

	if (X.size() == 1) {
		if (X[0] - '0' <= M) { cout << 1 << '\n'; }
		else { cout << 0 << '\n'; }
		return 0;
	}
		
	ll l = d, r = INF;
	while (l < r) {
		ll mid = (l + r + 1) >> 1;
		if (isBigger(mid)) { r = mid - 1; }
		else { l = mid; }
	}

	if (r < l) { cout << 0 << '\n'; return 0; }
	cout << (l - d) << '\n';
}
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define inf 1e9
#define linf 1e18
#define BASE 1000000
#define EPS 1e-10
#define PI acos(-1)
#define pii pair<int,int>
#define piii pair<int, pair<int, int> >
#define pdi pair<double,int>
#define vi vector<int>
#define fi first
#define se second
#define ALL(x) (x).begin(), (x).end()
#define ms(x,val_add) memset(x, val_add, sizeof(x))
#define pb(x) push_back(x)
#define make_unique(x) sort(ALL(x)) ; x.erase( unique(ALL(x)), x.end()) ;
#define dbg(x) do { cout << #x << " = " << x << endl; } while(0)
#define mp(x, y) make_pair(x, y)
#define minimize(x, y) x = min(x, y)
#define maximize(x, y) x = max(x, y)


/*** IMPLEMENTATION ***/
bool exitInput = false;
int ntest = 1, itest = 1 ;

const int dx[4] =
{
    -1, 0, 1, 0
};
const int dy[4] =
{
    0, 1, 0, -1
};
// const int dx[8] = {-2, -1, -1, +0, +0, +1, +1, +2};
// const int dy[8] = {+0, -1, +1, -2, +2, -1, +1, +0};

/** Knight Move **/
// const int dx[8] = {+1, +2, +2, +1, -1, -2, -2, -1};
// const int dy[8] = {+2, +1, -1, -2, -2, -1, +1, +2};

const ll Mod = 1000000007LL;
const int maxn = 500 + 5;
const int maxv = 500 + 5;
const int maxe = 10000 + 5;

ll addMod( ll &x, ll y )
{
    x = ( x + y ) % Mod;
}

int main()
{
#ifdef HOME
    freopen( "input.txt", "r", stdin );
    freopen( "output.txt", "w", stdout );
#endif // HOME
    ios_base::sync_with_stdio( false );
    cin.tie( NULL );
    cout << setprecision( 10 );
    cout << fixed;

    int A, B;
    cin >> A >> B;
    A = A + B;
    if(A >= 15 && B >= 8)
        cout << 1;
    else if(A >= 10 && B >= 3)
        cout << 2;
    else if(A >= 3)
        cout << 3;
    else
        cout << 4;


    return 0;
}

