#include <bits/stdc++.h>
using namespace std;
long digsum(long n, long b) {
  if (b == 1) return n;
  if (n < b) return n;
  return digsum(floor(n / b), b) + n % b;
}
int main(int argc, char *argv[]) {
  long n;
  long s;
  cin >> n;
  cin >> s;
  for (long i = 1; i <= sqrt(n); i++)
    if (digsum(n, i) == s) return cout << i << endl, 0;
  long res = n + 1;
  for (long i = 1; i <= sqrt(n - s); i++)
    if ((n - s) % i == 0) {
      if (digsum(n, i) == s) res = min(res, i);
      if (digsum(n, (n - s) / i) == s) res = min(res, (n - s) / i);
    }
  if (s == 1) res = min(res, n);
  if (res != n + 1)
    cout << res;
  else
    cout << -1;
}
