#include <bits/stdc++.h>
using namespace std;
long long tmp;
long long trans(long long b, long long n) {
  if (n < b) {
    return n;
  }
  return trans(b, n / b) + n % b;
}
vector<long long> divi;
int main() {
  long long n, s;
  cin >> n >> s;
  tmp = n;
  if (s == 1) {
    cout << n;
    return 0;
  }
  long long k = sqrt(n);
  for (int i = 2; i <= k; i++) {
    if (trans(i, tmp) == s) {
      cout << i;
      return 0;
    } else {
      tmp = n;
    }
  }
  for (int i = 1; i <= sqrt(n - s); i++) {
    if ((n - s) % i == 0) {
      divi.push_back(i);
      divi.push_back((n - s) / i);
      if (i * i == (n - s)) {
        divi.pop_back();
      }
    }
  }
  sort(divi.begin(), divi.end());
  for (int i = 0; i < divi.size(); i++) {
    if (divi[i] * divi[i] >= n && trans(divi[i], tmp) == s) {
      cout << divi[i];
      return 0;
    } else {
      tmp = n;
    }
  }
  cout << "-1";
  return 0;
}
