/**
 * 問題順: D
 * 問題名: Base n
 * 実行時間制限: 2 sec
 * メモリ制限: 1024 MB
 * 配点: 400点
 * URL: https://atcoder.jp/contests/abc192/tasks/abc192_d
 * 最終結果: WA
 * 問題分類: 数学, 数え上げ
 * アルゴリズム: 二分探索
 */
#include<iostream>
using namespace std;
using ll = long long;

string X;
ll M;
bool isOK(ll d) {
  ll x=0,tM=M;
  int i;
  int Mlen=0;
  while(tM>0) {
    Mlen++;
    tM/=d;
  }
  if (Mlen<X.size()) return false;

  for (i=0;i<X.size() && x<=M;i++) {
    x *= d;
    x += (X[i]-'0');
  }
  return x<=M;
}

int main() {
  int i;
  cin>>X>>M;
  int maxD=0;
  for (i=0;i<X.size();i++) {
    maxD = max(maxD, (int)(X[i]-'0'));
  }
  ll ans=0;
  ll ok = maxD+1, ng=1e18; ng++;
  if (X.size()>1) {
    if (isOK(ok)) {
      while (abs(ok - ng) > 1) {
        ll mid = (ok + ng) / 2;
        if (isOK(mid)) ok = mid;
        else ng = mid;
      }
      ans = ok-maxD;
    }
  } else {
    ans = isOK(ok) ? 1 : 0;
  }
  cout<<ans<<endl;
  return 0;
}

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

int sum_digit(int n){
    int res = 0;
    while(n){
        res += n%10;
        n /= 10;
    }
    return res;
}

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

    int a, b;
    cin >> a >> b;
    int x = sum_digit(a);
    int y = sum_digit(b);
    cout << (x > y ? x : y) ;
    cout << endl;

    return 0;
}
