#include <bits/stdc++.h>
using namespace std;
using lint = long long;
const lint LINF = 1e18;
lint digit_sum(lint n, lint b) {
  lint res = 0;
  while (n > 0) {
    res += n % b;
    n /= b;
  }
  return res;
}
int main() {
  lint n, s;
  cin >> n >> s;
  lint ans;
  if (n == s) {
    ans = n + 1;
  } else {
    ans = LINF;
    for (lint b = 2; b * b <= n; b++) {
      if (digit_sum(n, b) == s) {
        ans = min(ans, b);
      }
    }
    for (lint p = 1; p * p < n; p++) {
      if ((n - s) % p == 0) {
        lint b = lint(1) + (n - s) / p;
        if (0 <= p and p < b and 0 <= s - p and s - p < b) {
          ans = min(ans, b);
        }
      }
    }
  }
  if (ans == LINF) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
  return 0;
}
