#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
const int M = 1000000007;
long long n, s;
long long f(long long b, long long n) {
  if (b == 1) {
    return -1;
  }
  if (n < b) {
    return n;
  }
  return f(b, n / b) + n % b;
}
int main() {
  while (~scanf("%lld%lld", &n, &s)) {
    if (s == 1) {
      printf("%lld\n", n);
      continue;
    }
    long long ans = -1;
    for (long long b = 2,
                   bb = max(min(n - 1, 600000000ll), (long long)sqrt(n + 0.5));
         b <= bb; b++) {
      if (f(b, n) == s) {
        ans = b;
        break;
      }
    }
    if (ans != -1) {
      printf("%lld\n", ans);
      continue;
    }
    for (long long b = max(min(n - 1, 600000000ll), (long long)sqrt(n + 0.5));
         b >= 2; b--) {
      if (f(n + 1 - b, n) == s) {
        ans = n + 1 - b;
        break;
      }
    }
    printf("%lld\n", ans);
  }
}
