Public Class Program
  Shared Sub Main()
    Dim nk As Long() = Array.ConvertAll(Of String, Long)(Console.ReadLine().Split(" "), AddressOf Long.Parse)
    Dim n As Long = nk(0)
    Dim k As Long = nk(1)
    Dim arrayH As Long() = Array.ConvertAll(Of String, Long)(Console.ReadLine().Split(" "), AddressOf Long.Parse)
    Dim dp(0 To n - 1) As Long
    dp(0) = 0
    For j As Long = 1 To n - 1
      If j = 1 Then
        ' j = 1 
        dp(j) = Math.Abs(arrayH(j - 1) - arrayH(j))
      Else
        Dim jumpMin As Long = -1
        For i As Integer = 1 To k
          If i > j Then Exit For
          Dim jumpK As Long = Math.Abs(arrayH(j - i) - arrayH(j)) + dp(j - i)
          If jumpMin = -1 Then
            jumpMin = jumpK
          Else
            jumpMin = If(jumpK < jumpMin, jumpK, jumpMin)
          End If
        Next
        dp(j) = jumpMin
      End If
    Next
    Console.WriteLine(dp(n - 1).ToString())
  End Sub
End Class