import java.util.Scanner


import scala.io.StdIn._

object Main extends App {

  val Array(n,k) = readLine().split(" ").map(_.toLong)

  def solve() = {
    Math.min(n%k, k-(n%k))
  }

  println(solve())

}

