#include <bits/stdc++.h>
using namespace std;
long long MX = 400000;
long long solve(long long n, int bs) {
  long long sm = 0;
  while (n) {
    sm = sm + (n % bs);
    n = n / bs;
  }
  return sm;
}
int main() {
  long long n, s;
  cin >> n >> s;
  long long i = 2;
  for (i = 2; i * i <= n; i++) {
    if (solve(n, i) == s) {
      cout << i;
      return 0;
    }
  }
  for (; i > 0; i--) {
    long long a1 = i;
    long long a2 = s - i;
    if (a1 >= 0 && a2 >= 0) {
      long long tmp = n - a2;
      if (tmp % a1 == 0) {
        tmp = tmp / a1;
        if (tmp > a1 && tmp > a2) {
          cout << tmp;
          return 0;
        }
      }
    }
  }
  if (n == s) {
    cout << (n + 1) << endl;
    return 0;
  }
  cout << -1 << endl;
  return 0;
}
