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 inf = 1L<<60;

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

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

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

    long ans = inf;

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

    debug {
        if (ans < inf) {
            writefln("%d, %d == %d", ans, digitSum(n, ans), s);
        }
    }

    writeln(ans == inf ? -1 : ans);
}


long digitSum(long n, long b) {
    return n > 0 ? digitSum(n/b, b) + (n % b) : 0;
}











































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);
        }
    }
}
