fun main(args: Array<String>) = abc161c()

fun abc161c() {
    val (n, k) = readLine()!!.split(' ').map { it.toLong() }

    var currN = n % k
    val appeared = mutableSetOf(currN)
    var i = 0

    while (i++ < 10000000) {
        if (currN % k == 0L) return println(0)
        val diff = if (currN > k) currN % k else Math.abs(currN - k)
        appeared.add(diff)
        currN = diff
    }

    println(appeared.min()!!)
}
