#include <bits/stdc++.h>
using namespace std;
long long INF = 1e9;
long long MOD = 1e9;
long long N, S, D;
vector<long long> fact;
int main() {
  cin >> N >> S;
  D = N - S;
  if (N < S) {
    cout << -1;
    return 0;
  }
  for (long long i = 2; i * i <= N; ++i) {
    long long t = N;
    long long d = 0;
    while (t) {
      d += t % i;
      t /= i;
    }
    if (d == S) {
      cout << i;
      return 0;
    }
  }
  for (long long i = 1; i * i <= N && i <= S; ++i) {
    if (D % i == 0) {
      fact.push_back(D / i);
    }
  }
  reverse(fact.begin(), fact.end());
  for (auto v : fact) {
    long long b = v + 1;
    long long a = S / v;
    assert(a == N / b);
    long long c = N - a * b;
    if ((a + 1) * b <= N) continue;
    if (a * b > N) continue;
    assert(a + c == S);
    cout << b;
    return 0;
  }
  cout << -1;
}
