#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e15 + 7;
long long n, s;
long long f(long long b, long long nn) {
  if (nn < b) return nn;
  return f(b, nn / b) + nn % b;
}
int main() {
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  for (long long i = (2); i < (sqrt(n) + 1); ++i) {
    if (f(i, n) == s) {
      cout << i << endl;
      return 0;
    }
  }
  long long ans = INF;
  if (n - s <= 0) {
    cout << -1 << endl;
    return 0;
  }
  for (long long i = (1); i < (sqrt(n) + 1); ++i) {
    if (f((n - s) / i + 1, n) == s) {
      ans = min((n - s) / i + 1, ans);
    }
  }
  if (ans == INF) ans = -1;
  cout << ans << endl;
  return 0;
}
