#include <bits/stdc++.h>
using namespace std;
long long n, s;
int main() {
  cin >> n >> s;
  for (int i = 2; 1LL * i * i <= n; ++i) {
    long long val = 0;
    long long x = n;
    while (x > 0) {
      val += x % i;
      x /= i;
    }
    if (val == s) {
      cout << i << endl;
      return 0;
    }
  }
  if (s > n) {
    cout << -1 << endl;
    return 0;
  }
  long long result = 1e18 + 7;
  for (int i = 1; 1LL * i * i <= n - s; ++i) {
    if ((n - s) % i == 0) {
      long long val;
      {
        val = i + 1;
        if (n / val < val && (val - 1) * (n / val) == n - s) {
          result = min(result, val);
        }
      }
      {
        val = (n - s) / i + 1;
        if (n / val < val && (val - 1) * (n / val) == n - s) {
          result = min(result, val);
        }
      }
    }
  }
  if (result <= n) {
    cout << result << endl;
    return 0;
  }
  cout << -1 << endl;
  return 0;
}
