import scala.util.control.Breaks._

object Main extends App {
  val Array(n, k) = io.StdIn.readLine().split(' ').map(_.toLong)
  var x = n % k
  breakable {
    while(true) {
      val y = if (x < k) k - x else x - k
      if (x <= y) break() else x = y
    }
  }
  println(x)
}