#include <bits/stdc++.h>
using namespace std;
inline int R() {
  int rt = 0;
  bool isn = false;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar()) isn = ch == '-' ? true : isn;
  for (; ch >= '0' && ch <= '9'; ch = getchar()) rt = rt * 10 + ch - '0';
  return isn ? -rt : rt;
}
int main() {
  long long n, s;
  scanf("%lld%lld", &n, &s);
  if (n < s) {
    puts("-1");
    return 0;
  }
  long long lm = sqrt(n);
  for (int i = 2; i <= lm; ++i) {
    long long nans = 0;
    for (long long na = n; na; na /= i) nans += na % i;
    if (nans == s) {
      printf("%d\n", i);
      return 0;
    }
  }
  for (int i = lm; i; --i) {
    long long rem = n - s;
    if (!(rem % i)) {
      long long ans = rem / i + 1;
      if (n / ans != i) continue;
      printf("%lld\n", ans);
      return 0;
    }
  }
  puts("-1");
  return 0;
}
