import java.lang.Math

fun main(args: Array<String>) {
    val (n, k) = readInputLine().split(" ").map { it.toLong() }

    val tmp = n % k

    println(Math.min(tmp, Math.abs(tmp - k)))
}

fun readInputLine(): String {
    return readLine()!!
}
