#include <bits/stdc++.h>
double EPS = 1e-9;
using namespace std;
template <typename T, typename U>
inline void amin(T &x, U y) {
  if (y < x) x = y;
}
template <typename T, typename U>
inline void amax(T &x, U y) {
  if (x < y) x = y;
}
long long int MOD = 1000000007;
long long n, s;
bool check(long long b) {
  long long sum = 0;
  long long hoge = n;
  while (hoge > 0) {
    sum = sum + (hoge % b);
    hoge /= b;
  }
  if (sum == s)
    return true;
  else
    return false;
}
int main() {
  cin >> n >> s;
  long long m = sqrt(n) + 1;
  if (n < s) {
    cout << "-1" << endl;
  } else if (n == s) {
    cout << n + 1 << endl;
  } else {
    for (long long i = 2; i <= m; i++) {
      if (check(i)) {
        cout << i << endl;
        return 0;
      }
    }
    for (long long k = min(m, s); k > 0; k--) {
      if ((n + k - s) % k == 0) {
        if (check((n + k - s) / k)) {
          cout << (n + k - s) / k << endl;
          return 0;
        }
      }
    }
    cout << "-1" << endl;
  }
}
