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

#define ll long long int

const ll MAX = (ll)1e11 + 1ll;

bool check(ll b,ll n, ll s){
		ll sum = 0;
		while(n != 0){
				sum += n % b;
				n /= b;
		}
		if(sum == s)
			return true;
		else 
			return false;
}
int main(){
    ios_base::sync_with_stdio(false) , cin.tie(0) ,cout.tie(0);
	
	ll n,s;
	cin>>n>>s;
	
	if(n == s){
			cout<<(n+1);
			return 0;
	}
	else if(s > n){
			cout<<-1;
			return 0;
	}else{
			ll diff =  n - s;
			ll ans = MAX;
			ll sqr = (ll)(sqrt((double)diff));
			for(ll i=1;i<=sqr;i++){
					if(diff % i == 0){
						if(check(i+1,n,s))
							ans = min(ans , i+1);
						if(check(diff / i + 1 , n, s))
							ans = min(ans , diff/i+1);
					}
			} 
			
			if(ans == MAX){
				cout<<-1;
			}else
				cout<<ans;
				
	}
	return 0;
} 
