#include<bits/stdc++.h>

using namespace std;

#define ll long long int

ll f(ll b, ll n) {
	
	if(n < b)return n;
	
	return f(b,n/b) + n % b;
}

int main() {
	
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	
	ll n,s; cin >> n >> s;
	
	ll sq = sqrt(n);
	
	for(ll i = 2; i <= sq; i++) {
		if(f(i,n) == s) {
			cout << i << '\n';
			return 0;
		}
	}
	
	for(ll i = sq; i > 0; i--) {
		
		if(abs(n-s) % i != 0)continue;
		
		ll x = (n-s)/i + 1;
		
		if(x < 2)continue;
		if(x < sq)continue;
		if(x*i + (n%x) != n)continue;
		if(f(x,n) != s)continue;
			
		cout << x << '\n';
		return 0;
	}
	
	if(n == s) {
		cout << n+1 << '\n';
		return 0;
	}
	
	cout << -1 << '\n';
	
	return 0;
}