#include <bits/stdc++.h>
using namespace std;
int const N = 1e6 + 41;
long long const INF = N * 1LL * N;
long long n, s;
long long f(long long b, long long n) {
  if (n < b) return n;
  return n % b + f(b, n / b);
}
void solve() {
  long long ans = INF;
  cin >> n >> s;
  for (int i = 1; i < N; ++i) {
    long long cur = n / i;
    if (cur <= 1) continue;
    long long val = f(cur, n);
    long long nxt = n / (i + 1);
    long long cnt = cur - nxt - 1;
    if (cnt <= 0) continue;
    if (s >= val) {
      if ((s - val) % i == 0) {
        if ((s - val) / i <= cnt) {
          ans = cur - (s - val) / i;
        }
      }
    }
  }
  if (n == s) ans = min(ans, n + 1);
  if (ans == INF) ans = -1;
  if (ans != -1) {
    assert(s == f(ans, n));
  }
  cout << ans << endl;
}
int main() {
  solve();
  return 0;
}
