#include <bits/stdc++.h>
using namespace std;
long long f(long long b, long long n) {
  long long ret = 0;
  while (n >= b) {
    ret += n % b;
    n /= b;
  }
  ret += n;
  return ret;
}
signed main() {
  long long n, s;
  cin >> n >> s;
  bool flag = false;
  for (long long i = 2; i * i <= n; i++) {
    if (f(i, n) == s) {
      cerr << "i"
           << "=" << i << endl;
      cout << i << endl;
      return 0;
    }
  }
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  long long ans = -1;
  for (long long i = 1; i * i <= n + s; i++) {
    long long b = (n - s) / i + 1;
    if ((n - s) % i == 0 && s - i < b && s - i > 0 && b * b >= n) {
      long long p = i;
      if (p * b + (s - p) == n) {
        ans = b;
        cerr << "ans"
             << "=" << ans << endl;
      }
    }
  }
  if (ans != -1) {
    cout << ans << endl;
    return 0;
  }
  cout << -1 << endl;
}
