#include <bits/stdc++.h>
using namespace std;
long long ans, n, s;
void work1() {
  long long cur = n - s, p = sqrt(n);
  if (p * p == n) p++;
  if (n == s) ans = n + 1;
  for (int i = 1; 1ll * i * i <= cur; ++i)
    if (cur % i == 0) {
      long long b = i + 1;
      if (b >= p && n / b == cur / i) ans = min(ans, b);
      b = cur / i + 1;
      if (b >= p && n / b == i) ans = min(ans, b);
    }
}
void force() {
  for (long long b = 2; b * b <= n; ++b) {
    long long tmp = n, cur = 0;
    while (tmp) {
      cur += tmp % b;
      tmp /= b;
    }
    if (cur == s) {
      ans = min(ans, b);
      break;
    }
  }
}
int main() {
  cin >> n >> s;
  ans = n + 2;
  work1();
  force();
  if (ans == n + 2)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}
