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

const ll mod = 1e9 + 7;

ll digsum(ll n, ll b) {
  ll sum = 0;
  while (n != 0) {
    sum += n % b;
    n /= b;
  }
  return sum;
}

int main() {
  ll n, s;
  cin >> n >> s;
  ll maxb = sqrt(n);
  ll ans = LLONG_MAX;
  if (s == n) {
    cout << n+1 << endl;
    return 0;
  } else if (s > n) {
    cout << -1 << endl;
    return 0;
  }
  
  for (ll b = 2; b <= maxb; b++) {
    ll c = digsum(n, b);
    if (c == s) {
      ans = b;
      break;
    }
  }
  // n-s = p(b-1)
  ll x = n - s;
  ll p = 1;
  while (p <= maxb) {
    if (x % p == 0) {
      ll b = x/p + 1;
      if (digsum(n, b) == s && b != 1) {
        ans = min(b, ans);
      }
    }
    p++;
  }
  cout << (ans == LLONG_MAX ? -1 : ans) << endl;
  return 0;
}
