# 貰うDP

N,K=list(map(int,input().split()))

h=list(map(int,input().split()))

INF=10**9

dp=[INF]*(N)

dp[0]=0



for n in range(1,N):

    dp[n]=min([dp[k] + abs(h[n] - h[k]) for k in range(max(0,n-K), n)])

print((dp[N-1]))
# 配るDP

N,K=list(map(int,input().split()))

H=list(map(int,input().split()))

INF=10**9

add_list=[INF]*K



H.extend(add_list)



dp=[INF]*(N+100) #Kの分追加

dp[0]=0

for n in range(N):

    for k in range(n+1,n+K+1):

        dp[k]=min(dp[k], dp[n]+abs(H[n]-H[k]))

print((dp[N-1]))
# 貰うDP
N, K = list(map(int, input().split()))
h = list(map(int, input().split()))
INF = 10**9
dp = [INF] * (N)
dp[0] = 0
for n in range(1, N):
    dp[n] = min([dp[k] + abs(h[n] - h[k]) for k in range(max(0, n - K), n)])
print((dp[N - 1]))

# 配るDP
N, K = list(map(int, input().split()))
H = list(map(int, input().split()))
INF = 10**9
add_list = [INF] * K
H.extend(add_list)
dp = [INF] * (N + 100)  # Kの分追加
dp[0] = 0
for n in range(N):
    for k in range(n + 1, n + K + 1):
        dp[k] = min(dp[k], dp[n] + abs(H[n] - H[k]))
print((dp[N - 1]))

-# 貰うDP +# 配るDP -h = list(map(int, input().split())) +H = list(map(int, input().split())) -dp = [INF] * (N) +add_list = [INF] * K +H.extend(add_list) +dp = [INF] * (N + 100)  # Kの分追加 -for n in range(1, N): -    dp[n] = min([dp[k] + abs(h[n] - h[k]) for k in range(max(0, n - K), n)]) +for n in range(N): +    for k in range(n + 1, n + K + 1): +        dp[k] = min(dp[k], dp[n] + abs(H[n] - H[k]))