#include <bits/stdc++.h>
using namespace std;
const long long INF = 9223372036854775807ll;
long long n, s, ans = INF;
long long GetS(long long x) {
  long long f = n, ret = 0;
  while (f) ret += f % x, f /= x;
  return ret;
}
int main() {
  scanf("%lld%lld", &n, &s);
  if (n == s) {
    printf("%lld\n", n + 1ll);
    return 0;
  }
  long long T = n - s;
  for (long long i = 1; i <= sqrt(T); i++)
    if (T % i == 0) {
      long long x = i + 1ll, y = T / i + 1ll;
      if (GetS(x) == s) ans = min(ans, x);
      if (GetS(y) == s) ans = min(ans, y);
    }
  for (long long i = 2; i <= sqrt(n); i++)
    if (GetS(i) == s) {
      ans = min(ans, i);
      break;
    }
  printf("%lld\n", ans == INF ? -1ll : ans);
  return 0;
}
