#include <bits/stdc++.h>
using namespace std;
long long nextInt() {
  long long a;
  cin >> a;
  return a;
}
char nextChar() {
  char a;
  cin >> a;
  return a;
}
double nextDouble() {
  double a;
  cin >> a;
  return a;
}
string nextString() {
  string a;
  cin >> a;
  return a;
}
void inputVector(vector<long long> &v, long long &n) {
  for (long long i = 0; i < (n); i++) {
    v.push_back(nextInt());
  }
}
void inputVector(vector<double> &v, long long &n) {
  for (long long i = 0; i < (n); i++) {
    v.push_back(nextDouble());
  }
}
void inputVector(vector<string> &v, long long &n) {
  for (long long i = 0; i < (n); i++) {
    v.push_back(nextString());
  }
}
long long f(long long b, long long n) {
  if (n < b) return n;
  return f(b, n / b) + n % b;
}
signed main() {
  long long n, s;
  cin >> n >> s;
  if (n <= s) {
    cout << -1 << endl;
    return 0;
  }
  for (long long i = 2; i <= 100000000; i++) {
    if (f(i, n) == s) {
      cout << i << endl;
      return 0;
    }
  }
  if (f(n - s + 1, n) == s) {
    cout << n - s + 1 << endl;
  } else {
    cout << -1 << endl;
  }
  return 0;
}
