#include <bits/stdc++.h>
long long n, s;
bool check(long long base) {
  long long ret = 0, tmp = n;
  while (tmp) ret += tmp % base, tmp /= base;
  return ret == s;
}
int main() {
  scanf("%lld%lld", &n, &s);
  long long up = long long(sqrt(n)) + 1;
  for (long long b = 2; b <= up; b++)
    if (check(b)) return 0 * printf("%lld\n", b);
  if (n == s) return 0 * printf("%lld\n", n + 1);
  if (n < s) return 0 * puts("-1");
  long long tmp = n - s, upper = long long(sqrt(n - s)) + 1;
  for (long long b = 1; b <= upper; b++)
    if (tmp % b == 0) return 0 * printf("%lld\n", b + 1);
  return 0 * puts("-1");
}
