/* -------------------------------- Template -------------------------------- */

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <locale>
#include <iostream>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(x) (x).begin(),(x).end()

using ll = long long;
using ld = long double;

template <typename T> T &chmin(T &a, const T &b) { return a = std::min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = std::max(a, b); }

template<typename T> constexpr T inf = [](){ assert(false); };
// template<> constexpr int inf<int> = 1e9;
// template<> constexpr ll inf<ll> = 1e18;
// template<> constexpr ld inf<ld> = 1e30;

struct yes_no : std::numpunct<char> {
  string_type do_truename()  const { return "Yes"; }
  string_type do_falsename() const { return "No"; }
};

class range {
  class Index {
    int x;
  public:
    Index(int x) : x(x) {}
    int operator*() const { return x; }
    bool operator!=(Index &r) { return x < r.x; }
    void operator++() { ++x; }
  };
  Index i, n;
public:
  range(int n) : i(0), n(n) {}
  range(int i, int n) : i(i), n(n) {}
  Index &begin() { return i; }
  Index &end() { return n; }
};

template <class A, class B> class zip_t {
  using res_t = std::pair<typename A::iterator, typename B::iterator>;
  class iter : public std::iterator<std::input_iterator_tag,
                                    res_t,          // value_type
                                    std::ptrdiff_t, // difference_type
                                    res_t*,         // pointer
                                    res_t> {        // reference
    typename A::iterator a; typename B::iterator b;
  public:
    iter(typename A::iterator a, typename B::iterator b) : a(a), b(b) {}
    res_t operator*() { return res_t(*a, *b); }
    bool operator==(iter x) { return a == x.a; }
    bool operator!=(iter x) { return !(*this == x); }
    iter& operator++() { ++a; ++b; return *this; }
    iter operator++(int) { iter ret = *this; ++(*this); return ret; }
  };
  iter i, n;
public:
  zip_t(A &a, B &b) : i(a.begin(), b.begin()), n(a.end(), b.end()) {}
  iter &begin() { return i; }
  iter &end() { return n; }
};

template<typename A, typename B>
zip_t<A, B> zip(A &a, B &b) { return zip_t<A, B>(a, b); }

void solve();

int main() {
  std::locale loc(std::locale(), new yes_no);
  std::cout << std::boolalpha << std::setprecision(12) << std::fixed;
  std::cout.imbue(loc);
  solve();
  return 0;
}

using namespace std;

/* -------------------------------- Library -------------------------------- */

/* ---------------------------------- Main ---------------------------------- */

ll sum(ll n, ll b) {
  return n == 0 ? 0 : n % b + sum(n / b, b);
}

void solve() {
  ll N, S;
  cin >> N >> S;
  ll res = 1e14;
  if (N == S) chmin(res, N + 1);
  for (ll base: range(2, 500000)) {
    if (sum(N, base) == S) chmin(res, base);
  }
  for (ll i: range(1, 1000000)) {
    const ll one = i;
    const ll zero = S - i;
    if (zero < 0 || N - zero <= 0) continue;
    if ((N - zero) % one > 0) continue;
    ll base = (N - zero) / one;
    if (base > zero && base > one) chmin(res, base);
  }
  cout << (res == 1e14 ? -1 : res) << endl;
  return;
}


Measured memory_bytes: 256