
fun main(args: Array<String>) {
    val (N,K) = readLine()!!.split(" ").map(String::toLong)
    val x0 = if (N > K) { N} else {K - N}
    val x = x0 % K
    if(x <= K / 2) {x} else {K - x}.run(::println)
}