open System let ri () = stdin.ReadLine() |> int let ria () = stdin.ReadLine().Split() |> Array.map int type Sol() = member this.Solve() = let [|N;K|] = ria () let A = ria () let B = Array.sort A let mutable ans = 0 for i in 0..(N-1) do let n = Array.findIndex (fun x -> x = B.[i]) A if ( (n - i) % K ) = 0 then ( for j in 0..( (n-i)/K - 1 ) do ( let tmp = A.[(n - j*K)]; A.[(n - j*K)] <- A.[(n - j*K) - K]; A.[(n - j*K) - K] <- tmp; ans <- ans + 1 ) ) else ans <- -1000000000 if ans < 0 then ans <- -1 printfn "%d" ans let mySol = new Sol() mySol.Solve()