#include <bits/stdc++.h>
using namespace std;
long long solve(long long n, long long b) {
  long long r = 0;
  while (n) {
    r += n % b;
    n /= b;
  }
  return r;
}
int main() {
  long long n, s;
  scanf("%lld %lld", &n, &s);
  for (int b = 2; b <= 1000000; b++) {
    if (solve(n, b) == s) {
      printf("%d\n", b);
      return 0;
    }
  }
  long long ret = -1;
  for (long long a = 1; a <= 1000000 && a <= s; a++) {
    long long b = s - a;
    long long base = (n - b) / a;
    if (base == 1) continue;
    if (b >= base) continue;
    if (a >= base) continue;
    if (ret == -1) {
      ret = base;
    } else {
      ret = min(ret, base);
    }
  }
  printf("%lld\n", ret);
}
