#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  int64_t n, s;
  cin >> n >> s;
  for (int64_t i = 2; i * i <= n; i++) {
    int64_t sum = 0;
    for (int64_t t = n; t; t /= i) sum += t % i;
    if (sum == s) return cout << i << endl, 0;
  }
  for (int64_t i = ceil(sqrt(n)); i > 0; --i) {
    int64_t b = n / i, r = n % i;
    if (i >= b) continue;
    if (r + i == s) return cout << b << endl, 0;
  }
  cout << -1 << endl;
  return 0;
}
