#include <bits/stdc++.h>
using namespace std;
using ui = unsigned;
using ll = long long;
using ul = unsigned long long;
using ld = long double;
const int inf = 1e9 + 10;
const ll inf_ll = 1e18 + 10;
const ll mod = 1e9 + 7;
const ll mod9 = 1e9 + 9;
const int dx[]{-1, 0, 1, 0, -1, 1, 1, -1};
const int dy[]{0, -1, 0, 1, -1, -1, 1, 1};
template <class T, class U>
void chmin(T &x, const U &y) {
  x = min<T>(x, y);
}
template <class T, class U>
void chmax(T &x, const U &y) {
  x = max<T>(x, y);
}
struct prepare_ {
  prepare_() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(12);
  }
} prepare__;
int main() {
  ll n, s;
  cin >> n >> s;
  if (n == s) cout << n + 1 << endl, exit(0);
  ll res = inf_ll;
  for (ll i = (2); i < (ll)(n); i += (ll)1) {
    if (i * i > n) break;
    ll x = n, t = 0;
    while (x) t += x % i, x /= i;
    if (s == t) chmin(res, i);
  }
  for (ll i = (1); i < (ll)(n); i += (ll)1) {
    if (i * i > n) break;
    if ((n - (s - i)) % i) continue;
    ll b = (n - (s - i)) / i;
    if (s - i >= b) continue;
    if (i * b + s - i != n) continue;
    if (s - i < 0) continue;
    chmin(res, b);
  }
  cout << (res == inf_ll ? -1 : res) << endl;
}
