#include <bits/stdc++.h>
using namespace std;
long long func(long long n, long long b) {
  if (b == 1) return -1;
  long long ret = 0;
  while (n) {
    ret = ret + n % b;
    n = n / b;
  }
  return ret;
}
int main() {
  long long n, s;
  cin >> n >> s;
  long long i;
  long long ans = 1000000000000000000;
  for (i = 2; i <= 1000000; i++) {
    long long k = func(n, i);
    if (k == s) {
      cout << i << endl;
      return 0;
    }
  }
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  if (n < s) {
    cout << "-1" << endl;
    return 0;
  }
  for (i = 1; i * i <= (n - s); i++) {
    if ((n - s) % i) continue;
    long long b1 = i + 1;
    long long b2 = (n - s) / i + 1;
    if (func(n, b1) == s) ans = min(ans, b1);
    if (func(n, b2) == s) ans = min(ans, b2);
  }
  if (ans = 1000000000000000000) {
    cout << "-1";
    return 0;
  }
  cout << ans;
  return 0;
}
