fun main(arg: Array<String>) {
    val N = nextLong()
    val S = nextLong()
    if (N == S) return print(N + 1)
    val sqrtN = Math.sqrt(N.toDouble()).toLong()
    val list = mutableListOf<Long>()
    fun calC(b: Long, N: Long): Long {
        var n = N
        var cnt = 0L
        while (n > 0) {
            cnt += n % b
            n /= b
        }
        return cnt
    }
    for (n in 2 .. sqrtN)
        if (calC(n, N) == S) return print(n)
    val A = N - S
    if (A < 0) return print(-1)
    for (n in 1 .. sqrtN) {
        if (A % n == 0L) {
            list.add(n + 1)
            if (n != A / n) list.add(A / n + 1)
        }
    }
    list.sort()
    for (n in list) {
        if (n > sqrtN && calC(n, N) == S)
            return print(n)
    }
    return print(-1)
}

fun next() = readLine()!!
fun nextLong() = next().toLong()
