let rec helper n k loopNum acc =
    if loopNum = k + 1L
    then acc
    else
        let newN = (n - k) |> abs
        helper newN k (loopNum + 1L) (newN :: acc)

let judge n k = helper (n % k) k 0L [] |> List.min

//let input = [| 7L, 4L; 2L, 6L; 1000000000000000000L, 1L |]
//for n, k in input do (judge n k |> List.min |> 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
