#include <bits/stdc++.h>

using namespace std;

typedef long long i64;
typedef unsigned long long ui64;
typedef vector<i64> vi;
typedef vector<vi> vvi;
typedef pair<i64, i64> pi;

#define pb push_back
#define sz(a) i64((a).size())
#define all(c) (c).begin(), (c).end()
#define REP(s, e, i) for(i=(s); i < (e); ++i)
inline void RI(i64 &i) {scanf("%lld", &(i));}
inline void RVI(vi &v) { for(i64 i=0;i<sz(v);++i) { RI(v[i]); } }
inline void RVVI(vvi &vv) { for(i64 i=0;i<sz(vv);++i) { RVI(vv[i]); } }
inline void WI(const i64 &i) {printf("%lld\n", i);}
inline void WVI(const vi &v, char sep=' ') { for(i64 i=0;i<sz(v);++i) { if(i != 0){ printf("%c", sep); } printf("%lld", v[i]);} printf("\n"); }
inline void WS(const string &s) { printf("%s\n", s.c_str()); }
inline void WB(bool b, const string &yes, const string &no) { if(b){ WS(yes);} else { WS(no);} }
inline void YESNO(bool b) { WB(b, "YES", "NO"); }
inline void YesNo(bool b) { WB(b, "Yes", "No"); }
#define BUF_LENGTH 1000000
inline void RS(string &s) {static char buf[BUF_LENGTH]; scanf("%s", buf); s = buf;}
template<typename T> inline bool IN(T &S, const typename T::key_type &key) {
  return S.find(key) != S.end();
}
template<typename T> inline bool ON(const T &b, i64 idx) {
  return ((T(1) << idx) & b) != 0;
}

int main(int argc, char *argv[]) {
  i64 i, j, k;
  i64 L, R; cin >> L >> R;

  vi F(R+1, 0), G(R+1, 0);
  i64 g = R;
  auto CT = [&](i64 x, i64 g) {
    return max(0LL, x / g - 1);
  };
  auto DT = [&](i64 x, i64 g) {
    return x / g;
  };
  while(g > 1) {
    i64 C = CT(R, g) - CT(L-1, g);
    F[g] = C * (C - 1) / 2;
    i64 D = DT(R, g) - DT(L-1, g);
    G[g] = D * (D - 1) / 2;
    //cerr << g << " " << F[g] << " ";
    i64 k = 2;
    while(k * g <= R) {
      F[g] -= G[k * g];
      G[g] -= G[k * g];
      ++k;
    }
    //cerr << F[g] << endl;
    --g;
  }

  WI(accumulate(all(F), 0LL)*2);

  return 0;
}


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

char x[66];
int l,d;

bool ok(long long ts,long long n)
{
    long long now=0;
    for(int i=0;i<l;i++)
    {
        now+=x[i];
        if(now>ts)
            return 0;
        if(i<l-1 && now>ts/n)
            return 0;
        now=now*n;
    }
    return 1;
}

int main()
{
    long long m,L,R,Mid;
    scanf("%s%lld",x,&m);
    l=strlen(x);
    d=0;
    for(int i=0;i<l;i++)
    {
        x[i]-='0';
        d=max(d,x[i]-0);
    }
    if(l==1)
    {
        if(d<=m)
            printf("1\n");
        else
            printf("0\n");
    }
    else
    {
        L=d+1;
        R=m;
        while(L<R)
        {
            Mid=(L+R+1)/2;
            if(ok(m,Mid))
                L=Mid;
            else
                R=Mid-1;
        }
        if(L==R && ok(m,L))
            printf("%lld\n",L-d);
        else
            printf("0\n");
    }
    return 0;
}
