#include <bits/stdc++.h>
using namespace std;
constexpr long long INF = 1e18;
long long f(long long b, long long n) {
  if (n < b) {
    return n;
  } else {
    return f(b, n / b) + n % b;
  }
}
int main(void) {
  long long n, s;
  cin >> n;
  cin >> s;
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  for (long long b = 2; b * b <= n; b++) {
    long long ans = f(b, n);
    if (f(b, n) == s) {
      cout << b << endl;
      return 0;
    }
  }
  long long ans = INF;
  for (long long c = 1; c * c <= n; c++) {
    long long b = (n - s) / c + 1;
    if (b < 2) {
      continue;
    }
    cout << b << endl;
    if (f(b, n) == s) {
      ans = min(ans, b);
    }
  }
  cout << (ans == INF ? -1 : ans) << endl;
  return 0;
}
