#include <bits/stdc++.h>
long long n, s, b;
long long f(long long x, long long y) {
  long long ret = 0;
  while (x) {
    ret += x % y;
    x /= y;
  }
  return ret;
}
int main() {
  scanf("%lld%lld", &n, &s);
  if (n == s) {
    printf("%lld\n", n + 1);
    return 0;
  }
  if (s == 1) {
    printf("%lld\n", n);
    return 0;
  }
  for (long long i = 2; i * i <= n; i++) {
    if (f(n, i) == s) {
      printf("%lld\n", i);
      return 0;
    }
  }
  for (long long i = 1; i * i <= n; i++) {
    if ((n - s) % i == 0 && f(n, (n - s) / i + 1) == s) {
      printf("%lld\n", (n - s) / i + 1);
      return 0;
    }
  }
  printf("-1\n");
}
