#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
long long ans=1e18+5;
long long f(long long b,long long n) {
	if(n<b) {
		return n;
	} else {
		return f(b,n/b)+n%b;
	}
}
int m,n,s;
int main() {
	cin>>m>>s;
	if(s>m) {
		cout<<"-1";
		return 0;
	}
	if(s==m) {
		cout<<m+1<<endl;
	}
//	long long nn=sqrt(m)+1;
	for(long long x=2; x*x<=m; x++) {
	if(f(x,m)==s)
	{
		cout<<x<<endl;
		return 0;
	}
	}
	for(long long x=1;x*x<=m;x++)
	{
		if(m%x==0)
		{
			long long b=m/x+1;
			if(f(b,m+s)==s)
			{
				ans=min(ans,b);
			}
		}
	}
	if(ans==1e18+5)
	{
		cout<<-1<<endl;
	}
	else
	{
		cout<<ans<<endl;
	}
}