#include <bits/stdc++.h>
using namespace std;
long long n, s;
long long f(long long b, long long n) {
  if (n < b) return n;
  return f(b, n / b) + n % b;
}
signed main(void) {
  cin >> n >> s;
  if (n == s) return cout << n + 1, 0;
  for (long long i = 2; i <= (long long)sqrt(n); ++i)
    if (f(i, n) == s) return cout << i, 0;
  long long ans = 1e18;
  for (long long i = 1; i * i <= n; ++i)
    if ((n - s) % i == 0) {
      long long b = (n - s) / i + 1;
      long long x = i, y = s - i;
      if (x >= 0 && y >= 0 && b > sqrt(n) && x < b && y < b) ans = min(ans, b);
    }
  cout << (ans == 1e18 ? -1 : ans) << endl;
  return 0;
}
