import java.util.Scanner
import scala.math._

object Main extends App {
  val sc = new java.util.Scanner(System.in)
  val n,k = sc.nextBigInteger()
  val nmodk=n mod k
  println(if (nmodk.compareTo(k.subtract(nmodk)) < 0) nmodk else (k.subtract(nmodk)))
}