#include <bits/stdc++.h>
using namespace std;
int main() {
  long long N, S;
  long long ans = -1;
  cin >> N >> S;
  long long val = (long long)sqrt((double)N);
  for (long long i = 2; i <= val; i++) {
    long long sum = 0;
    long long n = N;
    while (n) sum += n % i, n /= i;
    if (!~ans && sum == S) ans = i;
  }
  for (long long i = val - (val * val == N); i > 0; i--) {
    long long minv = N % (N / i), maxv = N % (N / (i + 1) + 1);
    if (!~ans && minv <= S - i && S - i <= maxv) {
      ans = -(S - i - N) / i + minv;
    }
  }
  if (!~ans && N == S) ans = N + 1;
  cout << ans << endl;
  return (0);
}
