#include <bits/stdc++.h>
using namespace std;
long long f(long long b, long long n) {
  if (n < b) return n;
  return f(b, n / b) + n % b;
}
bool Check(long long b, long long n, long long s) {
  if (f(b, n) == s) return true;
  return false;
}
int main() {
  long long n, s;
  scanf("%lld %lld", &n, &s);
  if (s > n) {
    printf("-1\n");
    return 0;
  }
  if (s == n) {
    printf("%lld\n", n + 1LL);
    return 0;
  }
  long long n1 = n - s;
  long long ans = 100000000000000000LL;
  for (long long i = 1LL; i * i <= n1; i++) {
    if (n1 % i == 0) {
      if (Check(i + 1, n, s) == true) ans = min(ans, i + 1LL);
      if (n1 / i != i && Check(n1 / i + 1LL, n, s) == true)
        ans = min(ans, n1 / i + 1LL);
    }
  }
  if (ans == 100000000000000000LL)
    printf("-1\n");
  else
    printf("%lld\n", ans);
  return 0;
}
