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

#define INF (INT64_MAX)
#define MOD (1000000000+7)
#define MAX 50

long long f( long long b, long long n )
{
	long long res = 0;
	while( n ) {
		res += n % b;
		n /= b;
	}
	return res;
}

int main() {
	long long n, s;

	cin >> n >> s;

	long long ans = n + 1;
	if( n != s ) {
		int flag = 0;
		for( long long b = 2; b*b <= n; b++ ) {
			if( f( b, n ) == s ) {
				ans = b;
				flag = 1;
				break;
			}
		}
		if( flag == 0 ) {
			ans = INF;
			for( long long p = 1; p*p < n; p++ ) {
				long long b = (n - s) / p + 1;
				if( b < 2 ) {
					continue;
				}
				if( f( b, n ) == s ) {
					ans = min( ans, b );
				}
			}
			if( ans == INF ) {
				ans = -1;
			}
		}
	}

	cout << ans << endl;

	return 0;
}
