#include <bits/stdc++.h>
long long get(long long n, long long b) {
  if (n < b) {
    return n;
  }
  return get(n / b, b) + n % b;
}
int main(void) {
  long long n, s;
  scanf("%lld%lld", &n, &s);
  for (long long b = 2; b * b <= n; ++b) {
    if (get(n, b) == s) {
      printf("%lld\n", b);
      return 0;
    }
  }
  std::vector<long long> a;
  for (long long b = 1; b * b <= n - s; ++b) {
    if ((n - s) % b == 0 && b * b > n) {
      a.push_back(b);
      a.push_back((n - s) / b);
    }
  }
  a.push_back(n);
  a.push_back(n + 1);
  std::sort(a.begin(), a.end());
  for (auto b : a) {
    if (b <= 1) {
      continue;
    }
    if (get(n, b) == s) {
      printf("%lld\n", b);
      return 0;
    }
  }
  printf("-1\n");
  return 0;
}
