import java.math.*

fun main(args: Array<String>) {
    val numList = readLine()!!.split(" ")
    val numN = BigInteger(numList[0])
    val numK = BigInteger(numList[1])

    var modA = numN % numK
    var modB = numK - modA

    println(modA.min(modB))
}