#include <bits/stdc++.h>
#define REP(i, a, b) for (int i = a; i <= b; ++i)
#define PER(i, a, b) for (int i = a; i >= b; --i)
#define RVC(i, S) for (int i = 0; i < S.size(); ++i)
#define mp make_pair
#define pb push_back
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define fi first
#define se second
using namespace std;
 
typedef long long LL;
typedef pair<int, int> pii;
typedef vector<int> VI;
 
inline int read(){
	int x = 0, ch = getchar(), f = 1;
	while (!isdigit(ch)){if (ch == '-') f = -1; ch = getchar();}
	while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
	return x * f;
}

LL n, s;
VI tmp;

void check(LL x){
	tmp.clear();
	LL t = n;
	while (t){
		tmp.pb(t % x);
		t /= x;
	}
	int sum = 0;
	RVC(i, tmp) sum += tmp[i];
	if (sum == s){
		cout << x << endl;
		exit(0);
	}
}

int main(){
	cin >> n >> s;
	LL t;
	if (n == s){
		cout << n + 1 << endl;
		return 0;
	}
	LL ans = 1ll << 60;
	for (LL j, t = 1; t <= n; t = j + 1){
		LL p = n / t;
		j = n / p;
		// cerr << p << endl;
		
		if (n + p - s >= 0 && (n + p - s) % p == 0){
			LL b = (n + p - s) / p;
			if (0 <= p && p < b && 0 <= n - p * b && n - p * b < b){
				ans = min(ans, b);
			}
			return 0;
		}
	}
	if (ans == 1ll << 60){
		cout << b << endl;
	} else cout << -1 << endl;
	return 0; 
}