n,k = list(map(int,input().split()))
l = list(map(int,input().split()))
A = [0]*(n)
for i in range(1,k+1):
    A[i] = abs(l[i]-l[0])
for i in range(k,n):
    tmp = float('Inf')
    for j in range(1,k+1):
        dis = A[i-j] + abs(l[i]-l[i-j])
        tmp = min(tmp,dis)
    A[i] = tmp
print(A[n-1])