#include <bits/stdc++.h>
using namespace std;
long long N, S;
double n;
long long function(long long b, long long n) {
  if (n < b) {
    return n;
  } else {
    return function(b, n / b) + n % b;
  }
}
long long solve() {
  if (N < S) {
    return -1;
  }
  if (N == S) {
    return N + 1;
  }
  for (long long i = 2; i < n + 1; i++) {
    if (function(i, N) == S) {
      return i;
    }
  }
  for (long long p = (long long)(n + 1); p >= 1; p--) {
    if ((N - S) / p == 0) {
      continue;
    }
    if (N % p == S % p && function((N - S) / p + 1, N) == S) {
      return (N - S) / p + 1;
    }
  }
  return -1;
}
int main() {
  scanf("%lld", &N);
  scanf("%lld", &S);
  double N_double = N * 1.0;
  n = sqrt(N_double);
  long long ans = solve();
  printf("%lld", ans);
  return 0;
}
