#include <bits/stdc++.h>
const long double eps = 1e-2;
const long double pi = acos(-1.0);
using namespace std;
long long n, s;
long long f(long long b, long long n) {
  if (n < b)
    return n;
  else
    return (n % b) + f(b, n / b);
}
void solve() {
  cin >> n >> s;
  long long ans = 1e18;
  if (n == s) {
    cout << n + 1;
    return;
  }
  for (long long b = 2; b * b <= n; b++) {
    if (f(b, n) == s) ans = min(ans, b);
  }
  if (n - s > 0) {
    for (long long p = 2; p * p <= n - s; p++) {
      if ((n - s) % (p - 1)) continue;
      if (f((n - s) / (p - 1), n) == s) ans = min(ans, (n - s) / (p - 1));
    }
  }
  if (ans == 1e18)
    cout << -1;
  else
    cout << ans;
}
int main() {
  ios::sync_with_stdio(NULL), cin.tie(0), cout.tie(0);
  cout.setf(ios::fixed), cout.precision(3);
  int step;
  step = 1;
  for (int i = 1; i <= step; i++) {
    solve();
  }
}
