#include <bits/stdc++.h>
long long func(long long b, long long n) {
  if (n < b) {
    return n;
  }
  return n % b + func(b, n / b);
}
int main(int argc, char **argv) {
  long long n, s;
  std::cin >> n;
  std::cin >> s;
  const long long cond = 1000000;
  for (long long i = 2; i <= cond * 2; i++) {
    if (func(i, n) == s) {
      std::cout << i << std::endl;
      return 0;
    }
  }
  std::vector<long long> cand;
  if (n - s > 0) {
    for (long long i = 1; i <= cond + 50 && i <= s; i++) {
      if ((n - s) % i == 0) {
        long long bm1 = (n - s) / i;
        long long b = bm1 + 1;
        if (func(b, n) == s) {
          cand.push_back(b);
        }
      }
    }
    if (0 < cand.size()) {
      std::cout << cand[cand.size() - 1] << std::endl;
      return 0;
    }
  }
  if (n == s) {
    std::cout << n + 1 << std::endl;
    return 0;
  }
  std::cout << -1 << std::endl;
  return 0;
}
