#include <bits/stdc++.h>
using namespace std;
int _MAX(int a, int b) { return a > b ? a : b; }
int _MIN(int a, int b) { return a > b ? b : a; }
int main() {
  long long n, s, ans = -1, kk;
  scanf("%lld%lld", &n, &s);
  if (n < s) {
    puts("-1");
    return 0;
  }
  long long temp = (long long)sqrt(n + 0.5);
  for (long long i = 2; i <= temp; ++i) {
    long long val = 0, x = n;
    if (i != temp && (n - s) % i == 0) {
      kk = (n - s) / i + 1;
      if (n / kk + n % kk == s) ans = kk;
    }
    while (x) val += x % i, x /= i;
    if (val == s) {
      printf("%d\n", i);
      return 0;
    }
  }
  if (ans == -1) {
    kk = n - s + 1;
    if (n / kk + n % kk == s) ans = kk;
  }
  if (ans == 1) ans = -1;
  if (ans == -1 && n == s) ans = n + 1;
  printf("%lld\n", ans);
  return 0;
}
