#include<bits/stdc++.h>
#define endl '\n'
#define N 4000005
using namespace std;

long long a[N], s,ss,n,m,so;

int main(){
	ios::sync_with_stdio(0);
	cin.tie(NULL);cout.tie(NULL);
	cin>>n>>s;
	if(n<=s){
		cout<<-1;
		return 0;
	}
	so=1; a[1]=n-s+1;
	for(long i=sqrt(n-s);i>1;--i) if((n-s)%i==0){
		a[++so]=i+1; a[++so]=1+(n-s)/i;
	}
	sort(a+1,a+1+so,less<long>());
	for(long i=1;i<=so;++i){
		m=n; ss=0;
		while(m>0){
			ss+=m%a[i]; m/=a[i];
		}
		if(ss==s){
			cout<<a[i]; return 0;
		}
	}
	cout<<-1;
}