#include <bits/stdc++.h>

using namespace std;

long long f(long long b, long long n) {
    if (b <= 1) return -1;
    if (n < b) return n;
    else return f(b, n / b) + n % b;
}
int main() {
    long long n, s;

    scanf("%lld%lld", &n, &s);
    long long res = 1e13 + 1;

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

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

    long long sqrt = 0;
    for (; sqrt * sqrt <= n; sqrt++)
        ;
    for (long long p = sqrt; p > 0; --p) {
        long long r = s - p;
        long long b = (n - r) / p;
        if (b == 1) continue;
        if (f(b, n) == s) {
            cout << b << endl;
            return 0;
        }
    }

    cout << -1 << endl;


    return 0;
}

