#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll n, s;
ll calc(ll n, ll mid) {
  ll sum = 0;
  ll tmp = n;
  while (tmp) {
    sum += tmp % mid;
    tmp /= mid;
  }
  return sum;
}
int main() {
  cin >> n >> s;
  if (n < s) {
    cout << -1 << '\n';
    return 0;
  } else if (s == 1) {
    cout << n << '\n';
    return 0;
  }
  ll tmp = n - s;
  ll ans = -1;
  for (ll i = 2; i * i <= tmp; i++) {
    if (tmp % (i - 1)) continue;
    ll ret = calc(n, i);
    if (ret == s) {
      ans = i;
      break;
    }
  }
  cout << ans << '\n';
}
