#include <bits/stdc++.h>

using namespace std;

int64_t n, s;

int64_t f(int64_t b, int64_t n)
{
  if (n < b) return n;
  return f(b, n/b) + (n % b);
}

int main()
{
  cin >> n >> s;

  if (s > n) {
    cout << -1 << endl;
    return 0;
  }

  if (s == n) {
    cout << n+1 << endl;
    return 0;
  }

  for (int64_t b = 2; b*b <= n; b++) {
    if(f(b,n) == s) {
      cout << b << endl;
      return 0;
    }
  }

  int64_t min_b = -1;
  for (int64_t p = 1; p*p < n; p++) {
    int64_t b = (n - s)/p + 1;
    if (b < 2) continue;
    if (f(b,n) == s) {
      min_b = b;
    }
  }

  cout << min_b << endl;
  return 0;
}
