#include <bits/stdc++.h>
using namespace std;
inline void File() {
  freopen("D.in", "r", stdin);
  freopen("D.out", "w", stdout);
}
long long n, s;
inline long long calc(long long x, long long b) {
  long long res = 0;
  while (x) res += x % b, x /= b;
  return res;
}
int main() {
  cin >> n >> s;
  int lim = sqrt(n + 1);
  for (int i = 2; i <= lim + 1; ++i)
    if (calc(n, i) == s) return printf("%d\n", i), 0;
  for (int x1 = lim - 1; x1 >= 1; --x1) {
    if ((n - s) % x1 > 0) continue;
    long long b = (n - s) / x1 + 1;
    if (b > lim && x1 <= s && s - x1 < b) {
      return printf("%lld\n", b), 0;
    }
  }
  if (n == s) return printf("%lld", n + 1), 0;
  puts("-1");
  return 0;
}
