#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool chkmin(T &a, T b) {
  return b < a ? a = b, 1 : 0;
}
template <typename T>
inline bool chkmax(T &a, T b) {
  return b > a ? a = b, 1 : 0;
}
void File() {}
long long n, S;
inline bool Check(int val) {
  long long tmp = n, res = 0;
  for (; tmp; tmp /= val) res += tmp % val;
  return res == S;
}
int main() {
  File();
  cin >> n >> S;
  int gap = sqrt(n + .5);
  for (register int i = (2), iend = (int)(gap); i <= iend; ++i)
    if (Check(i)) return printf("%d\n", i), 0;
  for (long long i = gap + 1, Nexti; i <= n; i = Nexti + 1) {
    Nexti = n / (n / i);
    long long res = (S - n);
    if (res % (n / i)) continue;
    res = res / (n / i);
    long long b = -res + 1;
    if (i <= b && b <= Nexti) return printf("%lld\n", b), 0;
  }
  if (n == S) return printf("%lld\n", n + 1);
  puts("-1");
  return 0;
}
