#include <bits/stdc++.h>
using namespace std;
long long f(long long base, long long num) {
  long long res = 0;
  while (num) {
    res += num % base;
    num /= base;
  }
  return res;
}
int main() {
  std::ios_base::sync_with_stdio(false);
  long long n, s;
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  for (long long i = 2; i * i <= n; i++) {
    if (f(i, n) == s) {
      cout << i << endl;
      return 0;
    }
  }
  long long rt = sqrt(n);
  while (rt * rt < n) rt++;
  long long ans = LLONG_MAX;
  for (long long q = min(s, rt); q >= 1; q--) {
    long long r = s - q;
    if ((n - r) % q == 0) {
      long long b = (n - r) / q;
      if (r < b) {
        ans = min(ans, b);
      }
    }
  }
  if (ans == LLONG_MAX)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}
