#include <bits/stdc++.h>
using namespace std;
const long long MILLER_RABIN = 3215031751;
const long double PI = acos(-1);
inline void scan(int &x) { scanf("%d", &x); }
inline void scan(long long &x) { scanf("%lld", &x); }
inline void scan(double &x) { scanf("%lf", &x); }
inline void scan(long double &x) { scanf("%Lf", &x); }
inline void scan(int &x, int &y) { scanf("%d%d", &x, &y); }
inline void scan(long long &x, long long &y) { scanf("%lld%lld", &x, &y); }
inline void scan(int &x, int &y, int &z) { scanf("%d%d%d", &x, &y, &z); }
inline void scan(int &w, int &x, int &y, int &z) {
  scanf("%d%d%d%d", &w, &x, &y, &z);
}
inline void print(const int x) { printf("%d\n", x); }
inline void print(const long long x) { printf("%lld\n", x); }
inline void print(const double x) { printf("%lf\n", x); }
inline void print(const long double x) { printf("%Lf\n", x); }
inline void print(const int x, const int y) { printf("%d %d\n", x, y); }
inline void print(const int x, const int y, const int z) {
  printf("%d %d %d\n", x, y, z);
}
inline void tobin(long long x) { cout << bitset<63>(x) << endl; }
template <class T>
inline void trace(T collection) {
  for (auto elem : collection) {
    cout << elem << endl;
  }
}
long long n, s;
bool check_this_b(long long b) {
  long long sum = 0;
  long long nn = n;
  while (nn > 0) {
    sum += nn % b;
    nn /= b;
  }
  return sum == s;
}
void solve(int test) {
  scan(n, s);
  long long diff = n - s;
  long long ans = 1e18;
  for (long long i = 1; i * i <= diff; ++i) {
    if (diff % i == 0) {
      if (check_this_b(i + 1)) {
        ans = min(ans, i + 1);
      }
      if (check_this_b(diff / i + 1)) {
        ans = min(ans, diff / i + 1);
      }
    }
  }
  if (check_this_b(diff + 1)) {
    ans = min(ans, diff + 1);
  }
  print(ans == 1e18 ? -1 : ans);
}
int main() {
  int tests = 1;
  for (int test = 1; test <= tests; ++test) {
    solve(test);
  }
  return 0;
}
