#include <bits/stdc++.h>
int main() {
  long long n, m, i, s, j, a = 1e18;
  scanf("%lld %lld", &n, &m);
  if (n == m) {
    printf("%lld\n", n + 1);
    return 0;
  }
  for (i = 2; i * i <= n; i++) {
    s = 0;
    for (j = n; j; j /= i) s += j % i;
    if (s == m) goto END;
  }
  if ((i - 1) * (i - 1) == n) i--;
  for (j = i - 1; j; j--) {
    s = m - j;
    if (m < j || (n - s) % j) continue;
    i = (n - s) / j;
    if (s < i) goto END;
  }
  printf("-1\n");
  return 0;
END:;
  printf("%lld\n", i);
  return 0;
}
