#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;

#define N 200100
#define MOD 1000000007  //998244353
#define ll long long
#define rep(i, n) for(int i = 0; i < n; ++i)
#define rep2(i, a, b) for(int i = a; i <= b; ++i)
#define rep3(i, a, b) for(int i = a; i >= b; --i)
#define eb emplace_back
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define vi vector<int>
#define pii pair<int,int>
#define pll pair<ll,ll>
struct Setup_io {
	Setup_io() {
		ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
		cout << fixed << setprecision(15);
	}
} setup_io;



int main() {
	char a[100];
	ll b[100];
	ll c[100];
	int sz;
	ll n, x, y, z, l, r, mid;
	bool v;
	rep(i, 100) {
		a[i] = 0;
	}
	cin >> a;
	cin >> n;
	sz = strlen(a);
	if (sz == 1) {
		x = (ll)(a[0] - '0');
		if (x <= n)cout << 1 << endl;
		else cout << 0 << endl;
		return 0;
	}
	l = 0;
	rep(i, sz) {
		c[i] = (ll)(a[i] - '0');
		l = max(l, c[i]);
	}
	z = l;
	r = n + 1;
	while (l + 1 < r) {
		mid = (l + r) / 2;
		x = n;
		rep(i, 100)b[i] = 0;
		rep(i, sz) {
			b[sz - i - 1] = x % mid;
			x /= mid;
		}
		v = true;
		if (x == 0) {
			rep(i, sz) {
				if (c[i] < b[i]) {
					v = true;
					break;
				}
				if (c[i] > b[i]) {
					v = false;
					break;
				}
			}
		}
		if (v)l = mid;
		else r = mid;
	}
	cout << (l-z)<<endl;
	return 0;
}

#include<bits/stdc++.h>
#define        u64             uint64_t
#define        ll              long long
#define        endl            "\n"
#define        PI              acos(-1)
#define        fi              first
#define        si              second
#define        mkp             make_pair
#define        pb              push_back
#define        set0(arr)       memset(arr, 0, sizeof(arr))
#define        setinf(arr)     memset(arr, 126, sizeof(arr))
#define        all(x)          (x).begin(),(x).end()
#define        sz(v)           ((int)(v).size())
#define        IOS             ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
using namespace std;
using          pll        =      pair<ll, ll> ;
using          vl         =      vector<ll> ;
using          vpll       =      vector<pll> ;
using          mll        =      map<ll, ll> ;
using          mcl        =      map<char, ll> ;
using          msl        =      map<string,ll> ;
using          sl         =      set<ll> ;
using          sc         =      set<char> ;
using          dl         =      deque<ll> ;
const int N = 1e6+5 ;

ll      mod = 1e9+7 ;
vl      adj[N] ;
vpll    adjc[N] ;
ll      vis[N] ;
ll      arr[N] ;

ll muld(ll a,ll b)
{
       return (((a%mod)*(b%mod))%mod);
}
ll sumd(ll a,ll b)
{
       return (((a%mod)+(b%mod))%mod);
}



int main()
{
    IOS;
    ll a, b, c, d, n, m, p, x, y, z, i, j, k, f = 0, tc, cnt = 0, sum = 0, mul = 1, mi = 1e18, ma = -1e18, cs;
    string   str ;
    char     ch ;
    double   db ;
    ll       l, r ;
    //code

    cin>>a>>b;
    x=b;
    y=2*a+100;
    if(y>=x)
    {
           cout<<y-x<<endl;
    }
    else cout<<0<<endl;


    //code
    return 0;
}
