object Main extends App {
  val input = readLine
  val n = input.split(" ")(0).toLong
  val k = input.split(" ")(1).toLong
  
  if(n == k) println(0)
  else if(n < k) {
    println(Array(n, (n-k).abs).min)
  } else if(k < n) {
    println(Array(n%k, k-(n%k)).min)
  }
}