// tested by Hightail - https://github.com/dj3500/hightail
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;

long n, s;

void main() {
    scan(n);
    scan(s);

    if (n == s) {
        writeln(n + 1);
        return;
    }

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

    long ans = n + 1;

    for (long p = 1; p*p < n; p++) {
        long b = (n - s) / p + 1;

        if (f(b, n) == s) {
            ans = b;
        }
    }

    writeln(ans == n + 1 ? -1 : ans);
}

long f(long b, long n) {
    if (b < 2) return 0;
    return n < b ? n : f(b, n / b) + (n % b);
}

void scan(T...)(ref T args) {
    string[] line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}


void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}