#include <bits/stdc++.h>
using namespace std;
long long N, S, len;
long long f(long long b, long long n) {
  if (n < b)
    return n;
  else
    return f(b, n / b) + n % b;
}
int main() {
  cin >> N >> S;
  len = sqrt(N);
  if (2 > len) len = 2;
  for (long long i = 2; i <= len; i++) {
    if (f(i, N) == S) {
      cout << i << "\n";
      return 0;
    }
  }
  for (long long i = len - 1; i >= 1; i--) {
    long long x = N / i, k = S - i - N % x;
    if (x - k / i <= 0) continue;
    if (k % i == 0 && S == i + N % (x - k / i) && x - k / i > k / i) {
      cout << x - k / i;
      return 0;
    }
  }
  if (S == N)
    cout << N + 1;
  else
    cout << -1 << "\n";
}
