import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;

void main()
{
    auto n = readln.chomp.to!long;
    auto s = readln.chomp.to!long;

    long b = 2;
    for (; b^^2 <= n; ++b) {
        auto m = n;
        long t;
        while (m) {
            t += m % b;
            m /= b;
        }
        if (t == s) {
            writeln(b);
            return;
        }
    }
    auto c = n / (b-1);
    for (; c > 1; --c) {
        auto m = s - c;
        if (m < 0) continue;
        b = n / c;
        if ((n-m) % b == 0) {
            writeln(b);
            return;
        }
    }
    if (s == 1) {
        writeln(n);
        return;
    }
    writeln(-1);
}