import java.util.*

fun main(args: Array<String>) {
    val sc = Scanner(System.`in`)
    val N = sc.nextLong()
    val K = sc.nextLong()

    val x = N / K
    println(Math.min(Math.abs(N - K * x), Math.abs(N - K * (x + 1))))
}
