import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;

enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;


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

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

    long ans = inf6;

    for (long p = 1; p*p < n; p++) {
        auto q = s - p;
        if ((n - q) % p == 0) {
            long b = (n - q) / p;
            if (b > 1 && dsum(b, n) == s) {
                ans = min(ans, b);
            }
        }
    }

    if (ans < inf6) {
        writeln(ans);
        return;
    }

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

    writeln(-1);
}


long dsum(long b, long n) {
    return n < b ? n : dsum(b, n / b) + (n % b);
}





void scan(T...)(ref T args) {
    import std.stdio : readln;
    import std.algorithm : splitter;
    import std.conv : to;
    import std.range.primitives;

    auto line = readln().splitter();
    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);
        }
    }
}
