#include <bits/stdc++.h>
using namespace std;
int64_t n, s, b = 100000000000;
int64_t calc(int64_t b, int64_t n) {
  if (n < b) return n;
  return calc(b, n / b) + (n % b);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  if (s == 1) {
    cout << n << endl;
    return 0;
  }
  for (int i = 2; i <= sqrt(n); ++i) {
    if (s == calc(i, n)) {
      cout << i << endl;
      return 0;
    }
  }
  for (int i = sqrt(n - s); i >= 1; i--) {
    if ((n - s) % i == 0) {
      int64_t t = ((n - s) / i + 1);
      if (s == calc(t, n)) {
        b = min(b, t);
      }
    }
  }
  if (b != 100000000000)
    cout << b << endl;
  else
    cout << -1 << endl;
  return 0;
}
