#include <bits/stdc++.h>
using namespace std;
long long n, s;
inline long long calc(long long n, long long b) {
  if (!n) return 0;
  return calc(n / b, b) + n % b;
}
int main() {
  cin >> n >> s;
  if (n <= s) {
    cout << ((n < s) ? -1 : n + 1) << endl;
    return 0;
  }
  int lim1;
  for (long long i = 2; i * i <= n; ++i) {
    if (calc(n, i) == s) {
      cout << i << endl;
      return 0;
    }
    lim1 = i;
  }
  for (long long i = 1; i * i <= n - s; ++i) {
    if ((n - s) % i == 0) {
      int b = (n - s) / i + 1;
      if (calc(n, b) == s) {
        cout << b << endl;
        return 0;
      }
    }
  }
  puts("-1");
  return 0;
}
