#include <bits/stdc++.h>
using namespace std;
long long n, s;
long long sum(long long x) {
  long long tmp = n, ans = 0;
  while (tmp) {
    ans += tmp % x;
    tmp /= x;
  }
  return ans;
}
signed main() {
  cin >> n >> s;
  for (long long i = 2; i * i <= n; i++) {
    if (sum(i) == s) {
      cout << i;
      return 0;
    }
  }
  if (s == 1) {
    if (n == 1)
      cout << 2;
    else
      cout << n;
    return 0;
  }
  cout << -1;
}
