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

ull f(ull b, ull n) {
	if (n < b) { return n; }
	else { return f(b, floor(n / b)) + (n % b); }
}

int main() {
	ull n, s;
	cin >> n >> s;

	if (n == s) {
		cout << n + 1 << endl;
		return 0;
	}

	for (ull b = 2; b * b <= n; ++b) {
		if (f(b, n) == s) {
			cout << b << endl;
			return 0;
		}
	}

	ull res = 1;
	for (ull p = 1; p * p < n; ++p) {
		ull x = (n - s) / p + 1;
		if ((x * x <= n) || (x > n)) { continue; }

		if (f(x, n) == s) {
			if (res < 2) { res = x; }
			res = min(res, x);
		}
	}

	if (res < 2) { cout << -1 << endl; }
	else { cout << res << endl; }

	return 0;
}
