#include <bits/stdc++.h>
using namespace std;
const int NUM = 1e5 + 5;
const int mod = 998244353;
int INF = 2e9;
pair<int, int> pinf = {INF, INF};
long long f(long long b, long long n) {
  if (n < b) return n;
  return n % b + f(b, n / b);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  long long n, s;
  cin >> n >> s;
  for (long long i = 2; i * i <= n; i++)
    if (f(i, n) == s) {
      cout << i;
      return 0;
    }
  long long t = n - s;
  if (s == 1) {
    cout << max(n, (long long)2);
    return 0;
  }
  if (t <= 0) {
    cout << -1;
    return 0;
  }
  for (long long i = 1; i * i <= t; i++) {
    if (t % i != 0) continue;
    long long b = i + 1;
    long long x = t / i;
    long long y = s - x;
    if (max(x, y) < b) {
      cout << b;
      return 0;
    }
  }
  cout << -1;
  return 0;
}
