object Main extends App {
  val nk =  scala.io.StdIn.readLine()
  val nkArray = nk.split(" ").map(_.toLong).toSeq
  val n = nkArray(0)
  val k = nkArray(1)

  val amari = n % k
  val amariMinusK = Math.abs(n % k - k)

  println(if(amariMinusK < amari) amariMinusK else amari)
}