import io.StdIn._
import scala.math.pow
 
object Main extends App {
  val arr = readLine.split(" ")
  val n   = arr(0).toLong
  val k   = arr(1).toLong
 
  require(0 <= n && n <= pow(10, 18))
  require(1 <= k && n <= pow(10, 18))
 
  val a = n % k
  val b = k - a
 
  if (a > b) println(b) else print(a)
}