#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, s;
  cin >> n >> s;
  if (n < s) {
    cout << -1 << endl;
    return 0;
  }
  for (ll b = 2; b * b <= n; b++) {
    ll t = n, sum = 0;
    while (t > 0) {
      sum += t % b;
      t /= b;
    }
    if (sum == s) {
      cout << b << endl;
      return 0;
    }
  }
  ll b = (n == s ? n + 1 : -1);
  for (ll p = 1; p * p <= n; p++) {
    ll q = s - p;
    if (q < 0) break;
    ll t;
    if ((n - q) % p == 0) t = (n - q) / p;
    if (t > q && t > 1) b = t;
  }
  cout << b << endl;
  return 0;
}
