#include <bits/stdc++.h>
using namespace std;
long long kansu(long long b, long long n) {
  if (n < b) return n;
  return (n % b) + kansu(b, n / b);
}
long long calc(long long n, long long s) {
  long long ans = 0;
  long long k = sqrt(n - s);
  for (long long i = 1; i < k + 1; i++) {
    if (((n - s) / i) * i == (n - s) && ((n - s) / i) + 1 > (s - i) && s >= i) {
      ans = i;
    }
  }
  return ans;
}
int main() {
  long long n, s, ans;
  vector<long long> v;
  cin >> n >> s;
  if (n == s)
    cout << n + 1 << endl;
  else {
    if (n < s)
      cout << -1 << endl;
    else {
      long long a1, b1;
      a1 = calc(n, s);
      if (a1 != 0) {
        b1 = ((n - s) / a1) + 1;
        v.push_back(b1);
      }
      for (long long i = 2; i < 400000; i++) {
        if (kansu(i, n) == s) {
          v.push_back(i);
        }
      }
      if (v.size() == 0)
        cout << -1 << endl;
      else
        cout << *min_element(v.begin(), v.end()) << endl;
    }
  }
  return 0;
}
