#include <bits/stdc++.h>
using namespace std;
long long int f(long long int n, long long int b) {
  long long int a = 0;
  while (n > 0) {
    a += n % b;
    n = n / b;
  }
  return a;
}
int main() {
  long long int b, n, s, ans = -1;
  cin >> n >> s;
  if (s <= (n + 1) / 2) {
    long long int k = 1;
    bool flag = true;
    do {
      if (k * k > n - s || k == s) flag = false;
      if ((n - s) % k == 0) {
        b = (n - s) / k + 1;
        if (b <= s - k)
          flag = false;
        else
          ans = b;
      }
      k++;
    } while (flag);
  }
  b = 2;
  while (b * b < n) {
    if (f(n, b) == s) {
      ans = b;
      goto end;
    }
    b++;
  }
end:
  cout << ans << endl;
}
