#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll n, s;
  cin >> n >> s;
  if (n == s) {
    cout << (n + 1);
    return 0;
  } else if (n < s) {
    cout << "-1";
    return 0;
  }
  ll sqrtn = (ll)sqrt((double)n);
  for (ll b = 2; b <= sqrtn; ++b) {
    ll val = 0;
    ll m = n;
    while (m != 0) {
      val += m % b;
      m = m / b;
    }
    if (val == s) {
      cout << b;
      return 0;
    }
  }
  for (ll p = sqrtn; p >= 1; --p) {
    if ((n - s) % p != 0) continue;
    ll b = (n - s) / p + 1;
    ll val = 0;
    ll m = n;
    while (m != 0) {
      val += m % b;
      m = m / b;
    }
    if (val == s) {
      cout << b;
      return 0;
    }
  }
  if (s == 1)
    cout << n;
  else
    cout << "-1";
  return 0;
}
