#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const int64_t inf64 = 1e18;
const double eps = 1e-9;
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}
using i64 = int64_t;
i64 f(i64 b, i64 n) {
  if (n < b) return n;
  return f(b, n / b) + n % b;
}
void solve() {
  i64 n, s;
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << endl;
    return;
  }
  for (i64 b = 2; b * b <= n; ++b) {
    if (f(b, n) == s) {
      cout << b << endl;
      return;
    }
  }
  i64 ans = -1;
  for (i64 p = 1; p * p < n; ++p) {
    i64 b = (n - s) / p + 1;
    if (b > 2 and f(b, n) == s) ans = b;
  }
  cout << ans << endl;
}
int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(10);
  solve();
  return 0;
}
