#include <bits/stdc++.h>
int main(int argc, char *argv[]) {
  unsigned int n, b;
  unsigned int nn;
  unsigned int s;
  unsigned int a;
  int i;
  scanf("%d%d", &n, &s);
  if (s == 1) {
    printf("%d\n", n);
    return 0;
  }
  if (n == s) {
    printf("%d\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) break;
  }
  if (b == n) b = -1;
  if (a > s) b = -1;
  printf("%d\n", b);
  return 0;
}
