#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
template <typename T>
T &chmin(T &a, const T &b) {
  return a = min(a, b);
}
template <typename T>
T &chmax(T &a, const T &b) {
  return a = max(a, b);
}
ll n, s;
int f(ll b, ll n) {
  if (n >= b)
    return (int)(n % b) + f(b, (ll)(n / b));
  else
    return (int)n;
}
int main() {
  cin >> n >> s;
  ll nr = sqrt(n);
  for (ll b = 2; b < nr; b++) {
    int sum = f(b, n);
    if (sum == s) {
      cout << b << endl;
      return 0;
    }
  }
  if (s == n) {
    cout << n + 1 << endl;
    return 0;
  }
  if (s > 18) {
    cout << "-1" << endl;
    return 0;
  }
  for (int p = 1; p <= 9; p++) {
    ll q = s - p;
    if ((n - q) % p == 0 && 10 > q && q >= 0) {
      ll b = (n - q) / p;
      cout << b << endl;
      return 0;
    }
  }
  cout << "-1" << endl;
  return 0;
}
