let inp = readLine()!.split(separator: " ").map { Int($0)! }
let N = inp[0]
let K = inp[1]
let h = readLine()!.split(separator: " ").map { Int($0)! }
var dp: [Int] = .init(repeating: Int.max, count: N)
dp[0] = 0
for i in 0..<N-K {
  for j in 1...K {
  	dp[i + j] = min(dp[i + j], dp[i] + abs(h[i+j] - h[i]))
  }
}
print(dp[N-1])