fun main(args:Array<String>){
    val line = readLine()!!.split(" ").map{it.toLong()}
    val n = line[0]
    val k = line[1]
    val result = n%k
    if(result*2>k)println(k-result) else print(result)
}