#include <bits/stdc++.h>
using namespace std;
long long n, s;
long long f(long long b, long long n) {
  if (n < b) return n;
  return f(b, n / b) + n % b;
}
int main() {
  scanf("%lld%lld", &n, &s);
  if (n == s) {
    printf("%lld", n + 1);
    return 0;
  }
  long long root_n = sqrt(n);
  for (int i = 2; i <= root_n; i++) {
    if (f(i, n) == s) {
      printf("%d", i);
      return 0;
    }
  }
  long long st = n / (root_n + 1);
  for (int i = st; i >= 1; i--) {
    long long left = n / i, right;
    if (i == 1)
      right = n;
    else
      right = n / (i - 1) - 1;
    while (left > 1 && i == n / (left - 1)) left--;
    while (right <= n && i == n / (right + 1)) right++;
    while (left <= right) {
      long long b = (left + right) / 2;
      if (n / b + n % b == s) {
        printf("%lld", b);
        return 0;
      } else if (n / b + n % b > s)
        left = b + 1;
      else
        right = b - 1;
    }
  }
  printf("-1");
  return 0;
}
