#include <bits/stdc++.h>
using namespace std;
int f(int b, int n) { return (n < b) ? n : f(b, n / b) + n % b; }
int main(int argc, char *argv[]) {
  int n, s;
  cin >> n >> s;
  for (int b = 2; b <= sqrt(n); ++b) {
    if (f(b, n) == s) {
      cout << b << endl;
      return 0;
    }
  }
  for (int p = 1; p < sqrt(n); ++p) {
    int b = (n - s) / p + 1;
    if (1 < b and f(b, n) == s) {
      cout << b << endl;
      return 0;
    }
  }
  cout << -1 << endl;
  return 0;
}
