#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const long long INFF = 1e18;
int main(void) {
  long long n, s;
  cin >> n >> s;
  if (s == 1) {
    printf("%lld\n", n);
    return 0;
  } else if (s == n) {
    printf("%lld\n", n + 1);
    return 0;
  }
  long long ans = INFF;
  for (long long b = 2; b * b <= n; ++b) {
    long long t = n;
    long long d = 0;
    while (t > 0) {
      d += t % b;
      t /= b;
    }
    if (s == d) ans = (((ans) > (b)) ? (b) : (ans));
  }
  for (long long p = 1; p * p <= n - s; ++p) {
    if ((n - s) % p) continue;
    long long b = (n - s) / p + 1;
    long long t = n;
    long long d = 0;
    while (t > 0) {
      d += t % b;
      t /= b;
    }
    if (s == d) ans = (((ans) > (b)) ? (b) : (ans));
  }
  if (ans != INFF)
    printf("%lld\n", ans);
  else
    printf("-1\n");
  return 0;
}
