#include <bits/stdc++.h>
using namespace std;
long long f(long long b, long long n) {
  long long modsum = 0;
  while (n >= b) {
    modsum += (n % b);
    n = floor(n / b);
  }
  modsum += n;
  return modsum;
}
int main() {
  long long n, s;
  scanf("%lld", &n);
  scanf("%lld", &s);
  bool found;
  for (long long b = 2; b <= n; b++) {
    if (f(b, n) == s) {
      printf("%lld\n", b);
      found = true;
      break;
    }
  }
  if (not found) {
    printf("-1\n");
  }
  return 0;
}
