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