import java.lang.Math.abs

//package atcoder// TODO: ←消す

fun main(args: Array<String>) = ABC161C.main()

object ABC161C {
    fun log(message: Any?) {
        println(message)// TODO: ←消す
    }

    fun main() {
        val (N, K) = readLongs()

        var x = N
        while (true) {
            val next = next(x, K)
            if (x > next) x = next
            else break
        }
        println(x)
    }

    fun next(x: Long, K: Long): Long {
        return if (K <= x) {
            val minus = K * (x / K)
            if (minus != 0L) abs(minus - x) else abs(K - x)
        } else {
            abs(K - x)
        }
    }


    /*####################################################################################*/

    fun readLong() = readLine()!!.toLong()
    fun readLongs() = readLine()!!.split(" ").map { it.toLong() }
}