#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")
 
#include <bits/stdc++.h>
 
#define ll long long
#define ull unsigned long long
#define ld long double
#define rep(i, l, r) for (int i = l; i < r; i++)
#define repb(i, r, l) for (int i = r; i > l; i--)
#define sz(a) (int)a.size()
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define pb push_back
#define mp(a, b) make_pair(a, b)
#define ret(a) return cout<<a,0;
#define int long long
 
using namespace std;
 
mt19937 mrand(random_device{}()); 
const ll mod = 1e9 + 7;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b,ll mod) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
 
typedef pair < int , int > pii ;
#define y1 sfasfd
const int N = 3e5 + 123;
ll n , s , ans = 1e18;
ll f(ll b , ll n){
	if(n < b)
		return n;
	return f(b , n / b) + n % b;	
}
void SearchInSector(ll  l , ll r){
	if(l < 2) l = 2;
	if(r > n + 1) r = n + 1;
	if(r - l <= 3e8)
	for(int i = l ; i <= r ; i++){
		if(f(i , n) == s){
			ans = min(ans , i);return;
		}
	}}
main(){
	//n / b + n % b  == s
	//      
	cin >> n >> s;
	
	SearchInSector(	1 , 1e6);if(ans != 1e18) ret(ans);
	for(int i = 1; i  <= 1e6 ; i++){
		ll b = (n - s) / i + 1;
		if(b < 2)	continue;
		if(f(b , n) == s)
			ans = min(ans , b);
	}
	if(ans == 1e18)	ans = -1;
	cout << ans;
}