#include <bits/stdc++.h>
using namespace std;
long long n, s, b;
bool check(long long n, long long b) {
  long long res = 0;
  while (n > 0) {
    res += n % b;
    n /= b;
  }
  return res == s;
}
int main() {
  cin >> n >> s;
  long long temp = (long long)sqrt(n) + 1;
  for (long long i = 2; i <= temp; i++) {
    if (check(n, i)) {
      cout << i << endl;
      return 0;
    }
  }
  if (s == n) {
    cout << n + 1 << endl;
    return 0;
  }
  if (s > n) {
    cout << "-1" << endl;
    return 0;
  }
  long long temp1 = n - s, temp2 = temp1 / (temp - 1) + 1;
  for (int i = temp2; i >= 1; i--) {
    if (!(temp1 % i) && s - i <= temp1 / i && s >= i && i <= temp1 / i) {
      cout << temp1 / i + 1 << endl;
      return 0;
    }
  }
  cout << "-1" << endl;
  return 0;
}
