#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
using namespace std;
mt19937 mrand(random_device{}());
const long long mod = 1e9 + 7;
int rnd(int x) { return mrand() % x; }
long long powmod(long long a, long long b, long long mod) {
  long long res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
const int N = 3e5 + 123;
long long f(long long b, long long n) {
  if (n < b) return n;
  return f(b, n / b) + n % b;
}
int main() {
  long long n, s;
  cin >> n >> s;
  for (int i = 2; i <= min(10000000ll, n + 1); i++)
    if (f(i, n) == s) return cout << i, 0;
  ;
  long long ans = -1;
  for (long long i = n + 1; i > 1; i--) {
    if (f(i, n) == s) {
      ans = i;
    }
    if ((long double)clock() / (long double)CLOCKS_PER_SEC >= 1.9)
      return cout << ans, 0;
    ;
  }
  return cout << ans, 0;
  ;
}
