import java.util.Scanner

fun main(args: Array<String>) {
	val scn = Scanner(System.`in`)
	val n = scn.nextInt()
	val k = scn.nextInt()
	val sfs = IntArray(n){scn.nextInt()}
	val dp = IntArray(n)
	for (j in 1 until n){
		dp[j] = ((j-k).coerceAtLeast(0) until j).map { i ->
			dp[i] + Math.abs(sfs[i]-sfs[j])
		}.min()!!
	}
	println(dp.last())
}