#include <bits/stdc++.h>
using namespace std;
long f(long b, long n) {
  if (n < b) return n;
  long total = 0;
  while (n > 0) {
    total += n % b;
    n /= b;
  }
  return total;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long n, s;
  cin >> n >> s;
  if (n < s) {
    cout << -1 << endl;
    return 0;
  }
  if (s == n) {
    cout << s + 1 << endl;
    return 0;
  }
  for (long i = 2; i * i <= n; ++i) {
    int res = f(i, n);
    if (res == s) {
      cout << i << endl;
      return 0;
    }
  }
  long b_min = INT_MAX;
  for (long i = 1; i * i < n; ++i) {
    long b = (n - s) / i + 1;
    if (f(b, n) == s) {
      b_min = min(b_min, b);
    }
  }
  if (b_min == INT_MAX) b_min = -1;
  cout << b_min << endl;
  return 0;
}
