#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
constexpr long long MOD = 1000000000 + 7;
constexpr int INF = 2000000000;
constexpr int HINF = INF / 2;
constexpr double DINF = 100000000000000000.0;
constexpr long long LINF = 9223372036854775807;
constexpr long long HLINF = 4500000000000000000;
const double PI = acos(-1);
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
LL f(LL b, LL n) {
  if (n < b)
    return n;
  else
    return f(b, n / b) + n % b;
}
int main() {
  LL n, s;
  cin >> n >> s;
  LL i = 2;
  for (; i <= sqrt(n); i++) {
    if (f(i, n) == s) {
      cout << i << endl;
      return 0;
    }
  }
  LL a = n - s;
  if (a < 0) {
    cout << -1 << endl;
    return 0;
  } else if (a == 0) {
    cout << n + 1 << endl;
    return 0;
  }
  LL ans = LINF;
  for (; i >= 1; i--) {
    if (a % i != 0) continue;
    LL b = a / i + 1;
    if (f(b, n) == s) {
      ans = min(ans, b);
    }
  }
  if (ans != LINF) {
    cout << ans << endl;
  } else {
    cout << -1 << endl;
  }
}
