#include <bits/stdc++.h>
using namespace std;
long long N, S;
long long getSum(long long x, long long base) {
  long long ans = 0;
  while (x) {
    ans += x % base;
    x /= base;
  }
  return ans;
}
int main() {
  cin >> N >> S;
  if (S > N) {
    cout << -1 << endl;
    return 0;
  }
  if (S == N) {
    cout << N + 1 << endl;
    return 0;
  }
  for (long long b = 2; b * b <= N; b++) {
    if (getSum(N, b) == S) {
      cout << b << endl;
      return 0;
    }
  }
  for (long long i = sqrt(N - S); i > 0; i--) {
    long long b = (N - S) / i + 1;
    if ((N - S) % i) continue;
    if (getSum(N, b) == S) {
      cout << b << endl;
      return 0;
    }
  }
  cout << -1 << endl;
  return 0;
}
