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

ll f(ll b, ll n) {
  if (n < b)
    return n;
  else
    return f(b, n / b) + n % b;
}

int main() {
  ll n, s;
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }

  for (ll b = 2; b * b <= n; ++b) {
    if (f(b, n) == s) {
      cout << b << endl;
      return 0;
    }
  }

  // n == x * b + y
  // s == x + y
  ll ans = -1;
  for (ll x = 1; x * x < n; ++x) {
    ll b = (n - s) / x + 1;
    if (b >= 2 && f(b, n) == s) ans = b;
  }
  cout << ans << endl;

  return 0;
}
