#include <bits/stdc++.h>
using namespace std;
long long n, s;
long long f(long long b, long long n) {
  if (n < b) return n;
  return f(b, n / b) + n % b;
}
long long check(long long x) {
  long long b = x + 1;
  cout << "b = " << b << endl;
  if (b <= sqrt(n)) return 1e18;
  long long X = (n - s) / x;
  long long Y = s - X;
  cout << "X = " << X << " Y = " << Y << endl;
  if (X >= b || Y >= b || X < 0 || Y < 0) return 1e18;
  return b;
}
signed main(void) {
  cin >> n >> s;
  for (long long i = 2; i <= (long long)sqrt(n); ++i)
    if (f(i, n) == s) return cout << i, 0;
  long long ans = 1e18;
  for (long long i = 1; 1LL * i * i <= (n - s); ++i)
    if ((n - s) % i == 0) {
      long long b1 = check(i), b2 = check((n - s) / i);
      if (min(b1, b2) < ans) ans = min(b1, b2);
    }
  if (ans < 1e18) return cout << ans, 0;
  if (s == n) return cout << n + 1 << endl, 0;
  if (s == 1) return cout << n << endl, 0;
  cout << -1 << endl;
  return 0;
}
