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

typedef long long ll;

char X[1010];

ll m;
int len;

bool check(ll x)
{
    ll res = 0;
    for (int i = 1; i <= len; i++)
    {
        res = res * x + X[i] - '0';
        if (res > m || res < 0)
            return 0;
    }
    return 1;
}

int main()
{
    scanf("%s%lld", X + 1, &m);
    len = strlen(X + 1);
    if (len == 1)
    {
        if (X[1] - '0' <= m)
            puts("1");
        else
            puts("0");
        return 0;
    }
    ll l = 0, r = m, mid;
    int ans1 = 0;
    for (int i = 1; i <= len; i++)
        ans1 = max(ans1, X[i] - '0');
    ll ans2 = ans1;
    l = ans1 + 1;
    if (len > 2)
        r = sqrt(m) + 1;
    else
        r = m;
    while (l <= r)
    {
        mid = (l + r) / 2;
        if (check(mid))
        {
            l = mid + 1, ans2 = mid;
        }
        else
            r = mid - 1;
    }
    cout << ans2 - ans1 << endl;
    return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <math.h>
#include <queue>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <ctime>
#include <iomanip>
#include <fstream>
#include <random>
#include <numeric>
using namespace std;

#define ALL(c) (c).begin(),(c).end()
#define PB push_back
#define IN(x,c) (find(c.begin(),c.end(),x) != (c).end())
#define REP(i,n) for (int i=0;i<(int)(n);i++)
#define FOR(i,a,b) for (int i=(a);i<=(b);i++)
#define INIT(a,v) memset(a,v,sizeof(a))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
template<class A, class B> A cvt(B x) { stringstream ss; ss<<x; A y; ss>>y; return y; }

#define SPC << " " <<
#define DEBUG(x) cerr << #x << " = "; cerr << x << endl;
#define DEBUG_ITER(x) cerr << #x << " = "; for (auto _ : x) cerr << _ << ' '; cerr << endl;

typedef pair<int,int> PII;
typedef long long int64;
typedef vector<int> VI;
typedef vector<PII> VII;
typedef vector<double> VD;


int main() {
    //freopen("test.in","r",stdin);
    int64 b,c;
    cin >> b >> c;
    vector<pair<int64,int64> > seg;
    seg.PB({b-c/2, 1}); seg.PB({b+1, -1});
    if (c>=1) { seg.PB({-b-(c-1)/2, 1}); seg.PB({-b+(c-1)/2+1, -1}); }
    if (c>=2) { seg.PB({b-(c-2)/2, 1}); seg.PB({b+(c-2)/2+1, -1}); }
    sort(ALL(seg));
    int64 cov=0,y=-3e18;
    int64 r=0;
    for (auto [x, t] : seg) {
        if (cov>0) r+=x-y;
        cov+=t;
        y=x;
    }
    cout << r << endl;
    return 0;
}
