#include <bits/stdc++.h>
using namespace std;
long long cal(long long x, long long b) {
  long long t = x;
  long long sum = 0;
  while (t != 0) {
    sum += t % b;
    t /= b;
  }
  return sum;
}
int main() {
  long long n, s;
  cin >> n >> s;
  for (int i = 2; i < 2e6; i++)
    if (cal(n, i) == s) {
      cout << i << endl;
      exit(0);
    }
  cout << -1 << endl;
}
