fun main(args: Array<String>) {
    val line1 = readLine()!!.trim()
    val (n: Long, k: Long) = line1.split(" ").map {it.toLong()}

    var cur: Long = n % k

    while (true) {
        val next = skill(cur, k)
        if (next > cur) {
            break
        }

        cur = next
    }

    println(cur)
}

fun skill(value: Long, value2: Long): Long {
    val result =  (value2 - value)
    if (result < 0) {
        return - result
    } else {
        return result
    }
}