#include <bits/stdc++.h>
using namespace std;
long long f(long long b, long long n) {
  if (n < b)
    return n;
  else {
    return (f(b, floor(n / b)) + (n % b));
  }
}
set<long long> prime_factorization(long long n) {
  set<long long> ret;
  for (long long div = 2; div <= 1 + sqrt(n); div++) {
    while (n % div == 0) {
      ret.insert(div);
      n /= div;
    }
  }
  if (n != 1) ret.insert(n);
  return ret;
}
signed main() {
  long long n, s;
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  for (long long i = 2; i * i <= n; i++) {
    if (f(i, n) == s) {
      cout << i << endl;
      return 0;
    }
  }
  set<long long> st = prime_factorization(n - s);
  st.insert(n - s);
  long long ans = (1LL << 59);
  for (auto &elm : st) {
    long long B = elm + 1;
    long long p = (n - s) / (B - 1);
    long long q = s - p;
    if (q < 0) continue;
    if (p * B + q == n) ans = min(ans, B);
  }
  if (ans != (1LL << 59))
    cout << ans << endl;
  else
    cout << -1 << endl;
}
