#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 cc = n / i;
    long long oo = n / (i + 1) + 1;
    long long xx = n / cc + (n % cc);
    long long xx1 = n / oo + (n % oo);
    if (xx <= m && m <= xx1) {
      long long xxx = n / cc + (n % cc);
      long long chch = m - xxx;
      if (chch % i == 0) {
        printf("%lld\n", cc - chch / i);
        return 0;
      }
    }
  }
  puts("-1");
}
