fun main(args: Array<String>) {
    val (n, k) = readLine()!!.split(" ").map { it.toLong() }
    if (n == 0L || k == 1L || n == k) {
        println(0)
        return
    }
    if (n == 1L) {
        println(1)
        return
    }

    if ((n / k) <= 0) {
        val ans1 = Math.abs(n - k)
        val ans2 = Math.abs(k - ans1)
        val result = Math.min(n, Math.min(ans1, ans2))
        println(result)
        return
    }

    val ans1 = n % k
    val ans2 = k % ans1
    val result = Math.min(ans1, ans2)
    println(result)
}