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

ll n, s;

inline ll read() {
	ll s = 0, f = 1;
	char ch;
	for(; ch < '0' || ch > '9'; ch = getchar())	if(ch == '-')	f = -1;
	for(; ch >= '0' && ch <= '9'; ch = getchar())	s = (s << 1) + (s << 3) + ch - '0';
	return s * f;
}

inline ll f(ll b, ll n) {
	ll re;
	while(n) re += n % b, n /= b;
	return re;
}

int main() {
	n = read(), s = read();
	ll ans = -1;
	if(s == n)	ans = n + 1;
	ll m = sqrt(n) + 1;
	for(ll i = 2; i <= m; ++i)
		if(f(i, n) == s) { ans = i; break; }
	if(n > s && ans == -1) {
		n -= s;
		m = n / (n - 1) + 1;
		for(ll i = m; i >= 1; --i) {
			ll b = n / i + 1;
			if(!(n % i) && s >= i && (s - i) < b && i < b) {
				ans = b;
				break;
			}
		}
	}
	printf("%lld\n", ans);
	return 0;
}
