#include <bits/stdc++.h>
using namespace std;
long long f(long long b, long long n) {
  long long answer = 0;
  while (n >= b) {
    answer += n % b;
    n /= b;
  }
  return answer + n;
}
long long Solve(long long n, long long s) {
  long long prev = -1;
  int time_since_last_increase = 0;
  long long low = 100000000 + 1;
  for (int j = 2; j <= 100000000; j++) {
    long long curr = f(j, n);
    if (curr == s) {
      return j;
    }
    if (curr > prev) {
      time_since_last_increase = 0;
    } else {
      time_since_last_increase++;
    }
    prev = curr;
    if (time_since_last_increase >= 100000) {
      low = j;
      break;
    }
  }
  long long high = 1000000000000000000;
  while (low < high) {
    long long mid = (low + high) / 2;
    if (f(mid, n) >= s) {
      high = mid;
    } else {
      low = mid + 1;
    }
  }
  if (f(low, n) != s)
    return -1;
  else
    return low;
}
int main() {
  long long n, s;
  cin >> n >> s;
  cout << Solve(n, s) << endl;
  return 0;
}
