(*
ocamlfind ocamlopt -package batteries -linkpkg main.ml -o a.out
*)
open Batteries


let () = Scanf.sscanf (read_line ()) "%d %d" (
        fun n k -> 
                let t = n mod k in
                        if t < abs (t - k) then
                                t
                        else
                                abs (t - k)
) |> Printf.printf "%d\n"
