#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, m;
  scanf("%lld %lld", &n, &m);
  if (n == m) {
    printf("%lld", n + 1);
    return 0;
  }
  for (long long i = 2; i <= sqrt(n); i++) {
    long long xx = n;
    long long sum = 0;
    while (xx) {
      sum += (xx % i);
      xx /= i;
    }
    if (sum == m) {
      printf("%lld", i);
      return 0;
    }
  }
  for (long long i = sqrt(n) - 1; i >= 1; i--) {
    long long mid = i;
    long long cc = n / mid;
    long long oo = n / (mid + 1) + 1;
    if (i == sqrt(n) - 1) oo = sqrt(n) + 1;
    long long xx = n / cc + (n % cc);
    if (xx <= m && m <= xx + cc - oo + 1) {
      long long xxx = n / cc + (n % cc);
      m -= xxx;
      printf("%lld\n", cc - m);
      return 0;
    }
  }
  puts("-1");
}
