#include <bits/stdc++.h>
int main(int argc, char *argv[]) {
  long long n, b;
  long long nn;
  long long sn;
  long long s;
  long long a;
  int i;
  scanf("%lld%lld", &n, &s);
  if (n == s) {
    printf("%lld\n", n + 1);
    return 0;
  }
  if (n < s) {
    printf("%d\n", -1);
    return 0;
  }
  for (b = 2; b <= n; b++) {
    nn = n;
    a = 0;
    while (nn) {
      a += nn % b;
      nn = nn / b;
    }
    if (a == s) {
      printf("%lld\n", b);
      return 0;
    }
  }
  printf("-1\n");
  return 0;
}
