#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const long long INF = 1e+14;
const long double eps = 1e-8;
const long double pi = acos(-1.0);
void Yes() {
  cout << "Yes" << endl;
  exit(0);
}
void No() {
  cout << "No" << endl;
  exit(0);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long n, s;
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  if (s > n) {
    cout << -1 << endl;
    return 0;
  }
  long long b = 2;
  for (; b * b <= n; b++) {
    long long N = n;
    long long f = 0;
    while (N != 0) {
      f += N % b;
      N /= b;
    }
    if (f == s) {
      cout << b << "\n";
      return 0;
    }
  }
  b++;
  long long N = n - s;
  for (long long i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      if (i >= b - 1 && s >= i + 1 && s <= 2 * i + 1) {
        cout << i + 1 << endl;
        return 0;
      }
    }
  }
  for (long long i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      long long j = n / i;
      if (j >= b - 1 && s >= j + 1 && s <= 2 * j + 1) {
        cout << j + 1 << endl;
        return 0;
      }
    }
  }
  cout << -1 << endl;
  return 0;
}
