#include <bits/stdc++.h>
using namespace std;
long long n, s, ans;
vector<long long> vec;
long long f(long long b, long long n) {
  long long ret = 0ll;
  while (n >= b) {
    ret += n % b;
    n /= b;
  }
  ret += n;
  return ret;
}
int main() {
  scanf("%lld%lld", &n, &s);
  if (n == s) return !printf("%lld\n", n + 1);
  long long m = abs(n - s);
  for (long long i = 1; i * i <= m; i++) {
    if (m % i) continue;
    vec.push_back(m / i + 1);
    if (f(i + 1, n) == s) return !printf("%lld\n", i + 1);
  }
  reverse(vec.begin(), vec.end());
  for (auto i : vec)
    if (f(i, n) == s) return !printf("%lld\n", i);
  return !printf("-1\n");
}
