#include <bits/stdc++.h>
using namespace std;
inline long long f(long long b, long long n) {
  return n < b ? n : f(b, n / b) + n % b;
}
int main() {
  long long n, s, ans = -1, real = -1;
  scanf("%lld%lld", &n, &s);
  if (s == n) ans = n + 1;
  for (long long i = 1, b; i * i <= n; i++) {
    if (i > 1 && f(i, n) == s && (ans == -1 || ans > i)) ans = i;
    if ((n + i - s) % i == 0 && f((b = (n + i - s) / i), n) == s &&
        (ans == -1 || ans > b))
      ans = b;
  }
  printf("%lld\n", ans);
  return 0;
}
