n,k=gets.split.map(&:to_i)
h=gets.split.map(&:to_i)
dp=[0]
1.upto(n-1){|i|
  min=10**9
  [0,i-k].max.upto(i-1){|j|
    cost=dp[j]+(h[i]-h[j]).abs
    min=cost if cost < min
  }
  dp << min
}
puts dp.last