import java.util.Scanner

object Main {
  def main(args: Array[String]): Unit = {
    val sc = new Scanner(System.in)
    val n, k = sc.nextLong()
    println(math.min(n % k, math.abs(n % k - k)))
  }
}