#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) {
    return n;
  } else {
    f[b][n] = count(b, n / b) + n % b;
    return f[b][n];
  }
}
signed main() {
  long long n, s;
  cin >> n >> s;
  long long l = 1;
  long long r = 1e11;
  while (l != r) {
    long long mid = (l + r) / 2;
    long long val = count(mid, n);
    if (val == s) {
      cout << mid;
      return 0;
    } else if (val > s) {
      r = mid;
    } else {
      l = mid + 1;
    }
  }
  cout << "-1";
}
