#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
 
using namespace std;
using namespace __gnu_pbds;
 
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key
 
typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef long double ld; 
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
typedef set<int>::iterator sit;
typedef map<int,int>::iterator mit;
typedef vector<int>::iterator vit;

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

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	ll n, s; cin >> n >> s;
	for(ll i = 2; (i-2)*(i-2) <= n; i++)
	{
		if(digsum(n, i) == s)
		{
			cout << i; return 0;
		}
	}
	vector<ll> ans;
	for(ll i = 1; (i-2)*(i-2) <= n; i++)
	{
		ll s2 = s - i;
		if(s2 < 0) continue;
		if((n - s2)%i == 0)
		{
			ll q = (n-s2)/i;
			if(s2 < q && i < q)
			{
				ans.pb(q);
				//cout << i << ' ' << q << ' ' << s2 << '\n';
			}
		}
	}
	sort(ans.begin(), ans.end());
	if(ans.empty()) 
	{
		if(n == s) cout << n+1;
		else cout << -1;
	}
	else cout << ans[0];
}
