let judge (n:int64) (k:int64) = List.min [n % k |> abs; (k - (n % k) |> abs)]
//let input = [| 7L, 4L; 2L, 6L; 1000000000000000000L, 1L |]
//for n, k in input do (judge n k |> printfn "%d")
// expected 1; 2; 0

[<EntryPoint>]
let main argv =
    stdin.ReadLine().Split()
    |> Array.map int64
    |> fun x -> judge x.[0] x.[1]
    |> printfn "%d"
    0
