#include <bits/stdc++.h>
using namespace std;
vector<long long> vec;
long long n, s;
bool cmp(long long x, long long y) { return x > y; }
bool check(long long x) {
  long long a0, a1;
  a1 = n / x;
  a0 = n % x;
  if (a1 < x && x >= 2 && a0 + a1 == s) return true;
  return false;
}
int main() {
  cin >> n >> s;
  if (n < s) {
    cout << -1 << endl;
    return 0;
  }
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  long long i;
  for (i = 2; i * i <= n; i++) {
    long long x = n;
    long long sum = 0;
    while (x > i) {
      sum += x % i;
      x /= i;
    }
    sum += x;
    if (sum == s) {
      cout << i << endl;
      return 0;
    }
  }
  long long y = n - s;
  for (long long j = 1; j * j <= y; j++)
    if (y % j == 0) vec.push_back(j), vec.push_back(y / j);
  sort(vec.begin(), vec.end(), cmp);
  for (long long j = 0; j < vec.size(); j++) {
    long long b = y / vec[j] + 1;
    if (check(b)) {
      cout << b << endl;
      return 0;
    }
  }
  cout << -1 << endl;
  return 0;
}
