#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll minn=9999999999999;
ll n,s;
ll f(ll b,ll n)
{
	if(n<b)
		return n;
	else
		return f(b,n/b)+(n%b);
}
int main()
{
	ll be=minn;
	scanf("%lld %lld",&n,&s);
	if(n==s)
	{
		printf("%lld\n",n+1);
		return 0;
	}
	else if(s>n)
	{
		printf("-1\n");
		return 0;
	}
	ll maxn=sqrt(n)+1;
	for(ll i=2; i<=maxn; i++)
	{
		if(f(i,n)==s)
		{
			minn=i;
			break;
		}
	}
	ll del=n-s;
	for(ll i=1; i<=sqrt(del); i++)
	{
		if(del%i==0)
		{
			ll tem=del/i+1;
			if(f(tem,n)==s)
				minn=min(minn,tem);
		}
	}
	if(minn!=be)//zz
		printf("%lld\n",minn);
	else
		printf("-1\n");
	return 0;
}