#include <bits/stdc++.h>
using namespace std;
map<long long, map<long long, long long> > f;
long long count(long long b, long long n) {
  if (f[b][n] != 0) {
    return f[b][n];
  }
  if (n < b || b == 1) {
    return f[b][n] = n;
  } else {
    return f[b][n] = count(b, n / b) + n % b;
  }
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long n, s;
  cin >> n >> s;
  if (n == s) {
    cout << n + 1;
    return 0;
  }
  if (s == 1) {
    cout << n;
    return 0;
  }
  for (int i = 2; i * i <= n; i++) {
    if (count(i, n) == s) {
      cout << i;
      return 0;
    }
  }
  for (int i = sqrt(n) - 1; i >= 1; i--) {
    if (i > s) {
      continue;
    }
    long long val = n - (s - i);
    if (val % i != 0 || val / i == 1 || val / i <= (s - i)) {
      continue;
    }
    cout << val / i;
    return 0;
  }
  cout << "-1";
}
