open Batteries
let n, k = Scanf.sscanf (read_line()) "%d %d" (fun n k ->
  n, k
)

let _ = 
  (
    if n < k then n else
    if n = k then 0 else
    if n mod k = 0 then 0 else
    if abs((n mod k) - k) < n mod k then abs((n mod k) - k) else n mod k
  ) |> Printf.printf "%d\n"