#include <bits/stdc++.h>
using namespace std;
long long n, s;
int main(void) {
  scanf("%lld%lld", &n, &s);
  if (n == s) {
    printf("%lld\n", n + 1LL);
    return 0;
  }
  for (long long b = 2; b * b <= n; b++) {
    long long c = b, cnt = 0, nn = n;
    while (nn > 0) {
      cnt += nn % c;
      nn /= c;
    }
    if (cnt == s) {
      printf("%lld\n", b);
      return 0;
    }
  }
  long long res = n + 1LL;
  for (long long p = 1; p * p <= n; p++) {
    if ((n - s) % p == 0) {
      long long b = (n - s) / p + 1LL;
      if (b < n / b) continue;
      if (n / b + n % b == s) res = min(res, b);
    }
  }
  printf("%lld\n", res == n + 1LL ? -1 : res);
  return 0;
}
