#include <bits/stdc++.h>
long long N, Nsqrt, S;
std::vector<long long> candid{};
bool done;
long long f(long long b, long long n) {
  if ((b > n) || (b == 1)) return n;
  return f(b, n / b) + (n % b);
}
int main() {
  scanf(" %lld%lld", &N, &S);
  if (S == 1) return printf("%lld\n", N) & 0;
  if (S == N) return printf("%lld\n", N + 1) & 0;
  done = false;
  Nsqrt = sqrt(N);
  for (int i = 2; i <= Nsqrt; ++i)
    if (f(i, N) == S) {
      done = true;
      printf("%d\n", i);
      break;
    } else if (!(N % i) && (f(N / i + 1, N) == S))
      candid.push_back(N / i + 1);
  if (!done) {
    if (!candid.empty())
      printf("%lld\n", candid.back());
    else
      puts("-1");
  }
  return 0;
}
