#include <bits/stdc++.h>
using namespace std;
long long n, s, len;
inline bool check(long long x, long long tmp) {
  if (tmp == 1) return true;
  long long cnt = 0;
  while (x) cnt += x % tmp, x /= tmp;
  return cnt == s;
}
int main() {
  cin >> n >> s, len = (long long)sqrt(n) + 1;
  for (long long i = 2; i <= len; ++i)
    if (check(n, i)) {
      cout << i;
      return 0;
    }
  if (n <= s) {
    cout << (n < s ? -1 : n + 1);
    return 0;
  }
  long long tmp = n - s, ttmp = tmp / len + 1;
  for (int i = ttmp; i; --i)
    if (tmp % i == 0 && s >= i && s - i <= tmp / i && i <= tmp / i) {
      cout << tmp / i + 1;
      return 0;
    }
  cout << -1;
  return 0;
}
