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

fun min(a: Long, b: Long): Long{
    if(a < b) return a
    else return b
}
