#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = pair<int, int>;
using PLL = pair<ll, ll>;
const ll MOD = 1e9 + 7;
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
template <typename T>
void chmax(T &a, T &b) {
  a = max(a, b);
}
template <typename T>
void chmin(T &a, T &b) {
  a = min(a, b);
}
class Union_find {
 private:
  vector<int> par;
  vector<int> rank;
  int n;

 public:
  Union_find(int a) {
    n = a;
    for (int i = 0; i < n; i++) {
      par.push_back(i);
      rank.push_back(0);
    }
  }
  int find(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    if (rank[x] < rank[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (rank[x] == rank[y]) rank[x]++;
    }
  }
  bool same(int x, int y) { return find(x) == find(y); }
};
class RMQ {
  int n;
  vector<int> dat;

 public:
  RMQ(int n_) {
    n = 1;
    while (n < n_) n *= 2;
    for (int i = 0; i < (n * 2 - 1); i++) {
      dat.push_back(1e9);
    }
  }
  void update(int x, int i) {
    i += n - 1;
    dat[i] = x;
    while (i > 0) {
      i = (i - 1) / 2;
      dat[i] = min(dat[i * 2 + 1], dat[i * 2 + 2]);
    }
  }
  int query(int a, int b, int l, int r, int x) {
    if (a <= l && r <= b) return dat[x];
    if (r <= a || b <= l) return 1e9;
    int res = 1e9;
    res = min(query(a, b, l, (l + r) / 2, x * 2 + 1),
              query(a, b, (l + r) / 2, r, x * 2 + 2));
    return res;
  }
  int query(int a, int b) { return query(a, b, 0, n, 0); }
};
const ll mod = 1e9 + 7;
ll f(ll b, ll n) {
  ll sum = 0;
  while (n > 0) {
    sum += n % b;
    n = n / b;
  }
  return sum;
}
int main(void) {
  ll n, s;
  cin >> n >> s;
  if (n == s) {
    cout << n + 1 << endl;
    return 0;
  }
  ll ans = 1e18;
  for (int i = 2; i <= sqrt(n); i++) {
    if (f(i, n) == s) {
      ans = min(ans, (ll)i);
    }
  }
  for (int i = 1; i <= sqrt(n); i++) {
    ll b = (n - s) / i + 1;
    if (b > 1 && f(b, n) == s) {
      ans = min(ans, b);
    }
  }
  cout << (ans == 1e18 ? -1 : ans) << endl;
}
