#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const double eps = 1e-6;
const long long inf = 1e18;
const int mod = 1e9 + 7;
const int maxn = 5e5 + 10;
long long n, s;
long long f(long long x, long long b) {
  long long ret = 0;
  while (x) {
    ret += x % b;
    x /= b;
  }
  return ret;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> s;
  for (long long i = 2; (i - 1) * (i - 1) <= n; i++) {
    if (f(n, i) == s) {
      cout << i << endl;
      return 0;
    }
  }
  long long k = n - s;
  long long ans = inf;
  for (long long i = 1; i * i <= k; i++) {
    if (k % i == 0) {
      if (f(n, k / i + 1) == s) ans = min(ans, k / i + 1);
      if (f(n, i + 1) == s) ans = min(ans, i + 1);
    }
  }
  if (ans == inf) ans = -1;
  cout << ans << endl;
  return 0;
}
