#include <bits/stdc++.h>
using namespace std;
long long n, s;
bool check(long long x) {
  if (x < 2) return 0;
  long long k = n / x;
  long long r = n % x;
  if (s == k + r) return 1;
  return 0;
}
long long js(long long x, long long b) {
  long long ans = 0;
  while (x) {
    ans += x % b;
    x /= b;
  }
  return ans;
}
int main() {
  cin >> n >> s;
  long long b = 1e18;
  if (n == s) b = n + 1;
  if (n > s) {
    long long temp = n - s;
    for (long long t = 1; t <= sqrt(temp); t++) {
      if (temp % t == 0) {
        long long x = t + 1, y = temp / t + 1;
        if (check(x)) b = min(b, x);
        if (check(y)) b = min(b, y);
      }
    }
  }
  for (long long t = 2; t <= 1000000; t++) {
    long long cnt = js(n, t);
    if (cnt == s) b = min(b, t);
  }
  if (b == 1e18)
    printf("-1\n");
  else
    printf("%lld\n", b);
  return 0;
}
