#include <bits/stdc++.h>
using namespace std;
long long n, s;
long long gett(long long b) {
  long long k = n;
  long long ret = 0;
  while (k) {
    ret += k % b;
    k = k / b;
  }
  return ret;
}
int main() {
  cin >> n >> s;
  if (s > n) {
    cout << -1 << endl;
    return 0;
  }
  if (s == 1) {
    cout << n << endl;
    return 0;
  }
  for (long long i = 2; i <= 1000000; i++) {
    if (gett(i) == s) {
      cout << i << endl;
      return 0;
    }
  }
  vector<long long> divs;
  for (long long i = 1; i * i <= (n - s); i++) {
    long long K = n - s;
    if (K % i == 0) {
      if (K == i * i) {
        divs.push_back(i);
      } else {
        divs.push_back(i);
        divs.push_back(K / i);
      }
    }
  }
  sort(divs.begin(), divs.end());
  for (auto i : divs) {
    if (gett(i + 1) == s) {
      cout << i + 1 << endl;
      return 0;
    }
  }
  cout << "-1\n";
  return 0;
}
