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

ll n, s, sum = -1;

ll work(ll ni) {
	ll ans = 0;
	ll nn = n;
	while(nn > ni) ans += nn%ni, nn /= ni;
	return nn+ans;
}

int main() {
	scanf("%lld%lld", &n, &s);
	for(ll i = 2; i*i <= n; i++) {
		ll c = work(i);
		
		if(s == c) { sum = i; break; }
	}
	if(sum == -1) {
		ll c = n-s;
		for(ll i = c-1; i >= 2; i--) 
			if(c % i == 0) { sum = c/i+1; break; }
	}
	printf("%lld", sum);
	return 0;
} 