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

map<pair<long long, long long>, long long> mp;
long long func(long long b, long long n){
    if (b < 2 || n < 1) return 0;
    if (mp[{b, n}] != 0) return mp[{b, n}];
    if (b > n) return n;
    return func(b, n / b) + n % b;
}

int main(){
    long long n, s;
    cin >> n >> s;
    if (n == s) {
        cout << n + 1 << endl;
        exit(0);
    }
    for (int b = 2; b <= sqrt(n); b++){
        if (func(b, n) == s){
            cout << b << endl;
            exit(0);
        }
    }
    for (int p = (int)sqrt(n); p >= 1; p--){
        if ((n - s) % p == 0){
            if (func((n - s) / p + 1, n) == s){
                cout << (n - s) / p + 1 << endl;
                exit(0);
            }
        }
    }
    cout << - 1 << endl;
}