#include <iostream>
#include <algorithm>
#include <math.h>
#include <vector>
using namespace std;
typedef long long ll;
ll n, s;
vector<ll> vec;
int main(void){
    // Your code here!
    cin >> n >> s;
    if (n <= s) {
        cout << (n == s ? n + 1 : -1) << endl;
        return 0;
    }
    for (ll i = 1; i <= sqrt(n - s); i++){
        if ((n - s) % i) continue;
        vec.push_back(i);
        vec.push_back((n - s) / i);
    }
    sort(vec.begin(), vec.end());
    for (ll i = 0; i < vec.size(); i++){
        ll b = vec[i] + 1, ketawa = 0, warareru = n;
        while(warareru){
            ketawa += warareru % b;
            warareru /= b;
        }
        if (ketawa == s){
            cout << b << endl;
            return 0;
        }
    }
    cout << -1 << endl;
}
