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

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

int main()
{
	ll n, s;
	cin >> n >> s;
	if (n == s) {
		cout << n + 1 << endl;
		return 0;
	}
	for (ll b = 2; b <= (ll)sqrt((double)n); ++b) {
		if (f(b, n) == s) {
			cout << b << endl;
			return 0;
		}
	}
	for (ll p = (ll)sqrt((double)n); p > 0; --p) {
		ll b = (n - s) / p + 1;
		if (b < 2)
			continue;
		if (f(b, n) == s) {
			cout << b << endl;
			return 0;
		}
	}
	cout << -1 << endl;
	return 0;
}