#include <bits/stdc++.h>
using namespace std;
long long N, S, Max = 2;
inline bool chck(long long x) {
  long long Sum = 0;
  for (long long i = N; i;) {
    if (i >= x)
      Sum += i % x, i /= x;
    else {
      Sum += i;
      break;
    }
  }
  return Sum == S;
}
int main() {
  scanf("%lld%lld", &N, &S);
  for (long long i = 2; i * i <= N; i++)
    if (chck(i)) {
      printf("%lld\n", i);
      Max = i;
      return 0;
    }
  for (long long i = Max; i * i <= N - S; i++)
    if ((N - S) % i == 0)
      if (chck(i + 1)) {
        printf("%lld\n", i + 1);
        return 0;
      }
  for (long long i = sqrt(N - S); i >= 1; i--)
    if ((N - S) % i == 0)
      if (chck((N - S) / i + 1)) {
        printf("%lld\n", (N - S) / i + 1);
        return 0;
      }
  if (N == S)
    if (chck(N + 1)) {
      printf("%lld\n", N + 1);
      return 0;
    }
  puts("-1");
  return 0;
}
