#include <bits/stdc++.h>
void check(int64_t N, int64_t S, int64_t B) {
  int64_t sum = 0;
  for (; N; N /= B) {
    sum += N % B;
  }
  if (sum == S) {
    printf("%ld\n", B);
    exit(0);
  }
}
int main() {
  int64_t N, S;
  scanf("%ld %ld", &N, &S);
  if (S > N) {
    printf("-1\n");
    return 0;
  }
  if (S == N) {
    printf("%ld\n", N + 1);
    return 0;
  }
  int64_t i = 1;
  for (; i * i <= (N - S); i++) {
    if ((N - S) % i == 0) check(N, S, i + 1);
  }
  for (; i > 0; i--) {
    if ((N - S) % i == 0) check(N, S, (N - S) / i + 1);
  }
  printf("-1\n");
  return 0;
}
