fun main(arr:Array<String>) {
    val (n,k) = readLine()!!.split(" ").map { it.toLong() }
    println(getAns(n,k))
}


fun getAns(default:Long, k:Long):Long {
    val tmp = default % k
    if(tmp <= k / 2) {
        return tmp
    } else {
        return k-tmp
    }
}