module IntMap = Map.Make (struct
  type t = int
  let compare = compare
end)

let () = Scanf.scanf "%d %d\n" @@ fun n k ->
  let hs = Array.init n @@ fun _ -> Scanf.scanf "%d " @@ fun h -> h in
  let dp = Array.make n None in
  let q = ref @@ IntMap.singleton 0 [0] in
  let rec dijkstra () =
    let (w, us) = IntMap.min_binding !q in
    match dp.(n - 1) with
    | Some w' when w' <= w -> w'
    | _ ->
        q := IntMap.remove w !q;
        List.iter (fun u ->
          for v = u + 1 to min (u + k) (n - 1) do
            let c = abs (hs.(u) - hs.(v)) in
            if
              match dp.(v) with
              | None -> true
              | Some w' -> w + c < w'
            then begin
              dp.(v) <- Some (w + c);
              q := IntMap.add (w + c) (v :: try IntMap.find (w + c) !q with Not_found -> []) !q
            end
          done) us;
        dijkstra () in
  Printf.printf "%d\n" @@ dijkstra ()

