import java.util.*

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

  println(abc161c(n, k))
}

private fun abc161c(n: Long, k: Long): Long {

  if (n <= k) {
    var ans = n
    val ansList = mutableListOf<Long>()
    while (!ansList.contains(ans)) {
      ansList.add(ans)
      ans = diff(ans, k)
    }
    return ansList.min()!!
  }

  val d = n % k
  val ans = Math.min(d, diff(d, k))

  return ans

}

private fun diff(n: Long, k: Long): Long {
  if (n > k) return n - k

  return k - n
}
