#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

typedef pair<int, int> pii;
typedef pair<double,double> pdd;
typedef vector<int> vi;
typedef vector<pair<int, int > > vii;
typedef vector<ll> vll;
typedef vector<ld> vld;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
typedef vector<vll> vvll;
typedef map<int, int> mii;
typedef set<int> si;
typedef set<ll> sll;

#define Sort(x) sort(x.begin(),x.end())
#define Reverse(x) reverse(x.begin(),x.end())
#define ABS(a,b) ((a)<(b)?(b)-(a):(a)-(b))

// constexpr int INF = 1<<25;
// constexpr ll  INF = 1LL<<50;
// int dp[N_MAX][M_MAX] = {}

// Forward declaration
template <typename T>
void dumpContents(const T& v, const string& msg="");

template <typename T>
void dumpContents(const T& v, const string& msg)
{
  cerr << "### " << msg << " ###\n";
  for (const auto& x : v) {
    cerr << x << " ";
  }
  cerr << endl;
}

struct before_main_function {
  before_main_function() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout<<setprecision(20)<<fixed;
    #define endl "\n"
  }
} before_main_function;

bool isPrime(const long n)
{
  if (n <= 1)
    return false;
  else if (n < 4)
    return true;
  else if (n % 2 == 0)
    return false;
  else if (n < 9)
    return true;
  else if (n % 3 == 0)
    return false;

  for (long f=5; f*f<=n; f+=6) {
    if (n % f == 0)
      return false;
    else if (n % (f+2) == 0)
      return false;
  }
  return true;
}
vll eratosthenesPrimes(const long N)
{
  vll ret;
  vll tmp(N+1, 1);
  tmp[0] = 0;
  tmp[1] = 0;
  tmp[2] = 1;

  if (N<=1)
    return ret;
  ret.push_back(2);
  if (N<=2)
    return ret;

  for(long i=3; i<=N; i+=2) {
    if (tmp[i] == 0) continue;
    ret.push_back(i);
    if (N<i*i) continue;
    for (long k=i+i; k<=N; k+=i) {
      tmp[k]=0;
    }
  }
  return ret;
}
// A return set
// * does not include 1
// * includes n
//
// 2 -> {2}
// 6 -> {2, 3}
// 16 -> {2}  // uniqueなもののみ
//
// 約数を知りたい場合は、divisors()
sll uniqueFactors(long n)
{
  sll ret;
  if (n==1) return ret;

  const vll primes = eratosthenesPrimes(sqrt(n));
  for (const ll p : primes) {
    if (n==1) break;
    if (n % p == 0) {
      ret.insert(p);
      while (n % p == 0) {
	n /= p;
      }
    }
  }
  if (n>1) ret.insert(n);
  return ret;
}
map<long,long> factorExponents(long n)
{
  map<long, long> ret;
  if (n==1) return ret;

  const sll primes = uniqueFactors(n);
  for (const ll p : primes) {
    if (n % p == 0) {
      ret.insert(make_pair(p,0));
      while (n % p == 0) {
	n /= p;
	ret[p] += 1;
      }
    }
  }
  if (ret.empty()) ret.insert(make_pair(n,1)); // n is prime
  return ret;
}
/*
 * Rutuns a vector of sorted products of b^0, b^1, ... , b^n"
 */
vll makeProducts(const ll base, const ll n)
{
  vll ret;
  ret.reserve(n+1);
  ret.push_back(1);
  for (ll i=1; i<=n; ++i) {
    ret.push_back(ret.back()*base);
  }
  return ret;
}
/*
 * Returns a sorted sequence of divisors of n.
 * For example, 12 => (1 2 3 4 6 12)"
 */
vll divisors(long n)
{
  vll ret;
  ret.push_back(1);
  for (const auto& kv : factorExponents(n)) {
    const vll products = makeProducts(kv.first, kv.second);
    vll tmp;
    tmp.reserve(ret.size()*products.size());
    for (const ll x : ret) {
      for (const ll y : products) {
	tmp.push_back(x*y);
      }
    }
    ret.swap(tmp);
  }
  std::sort(ret.begin(), ret.end());
  return ret;
}

// ========== end of template ==========

ll sum(ll n, ll b) {
  ll ret = 0;
  while (n > 0) {
    ret += n%b;
    n /= b;
  }
  return ret;
}

int main(int argc, char** argv) {
  ll n,s;
  cin >> n >> s;

  sll ret;

  if (s==n) {
    ret.insert(s+1);
  } else if (n>s){
    ll d = ABS(n,s);
    for (ll c : divisors(d)) {
      ll b = c+1;
      if (s == sum(n,b)) {
        ret.insert(b);
      }
    }
  }

  if (ret.empty()) {
    cout << -1 << endl;
  } else {
    cout << *ret.begin() << endl;
  }

  return 0;
}


Measured memory_bytes: 3072