#include <bits/stdc++.h>
using namespace std;
long long n, s;
long long f(long long x, long long y) {
  if (y < x)
    return y;
  else
    return f(x, y / x) + (y % x);
}
int main() {
  scanf("%lld%lld", &n, &s);
  if (s > n) {
    printf("-1\n");
  } else if (n == s) {
    printf("%lld\n", n + 1);
  } else {
    long long ans = -1;
    for (long long i = s + 1; i <= s + 1000; i++) {
      if (f(i, n) == s) {
        ans = i;
      }
    }
    for (long long i = s - 1; i >= max(s - 1000, (long long)2); i--) {
      if (f(i, n) == s) {
        ans = i;
      }
    }
    if (s == 1) ans = n;
    printf("%lld\n", ans);
  }
  return 0;
}
