#atcoder https://atcoder.jp/contests/dp/tasks/dp_b



def main():

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

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





    dp = [0] * N

    dp[0] = 0



    for i in range(1, N):



        dp[i] = min(

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

        )



    print((dp[N-1]))



main()
def INT(): return int(eval(input()))

def MAP(): return list(map(int, input().split()))

def LIST(): return list(map(int, input().split()))



N, K = MAP()

h = LIST()



INF = 10**23



dp = [INF] * N

dp[0] = 0



for i in range(1, N):

    for j in range(1, K+1):

        if i - j < 0:

            continue

        dp[i] = min(dp[i], dp[i-j] + abs(h[i] - h[i-j]))



print((dp[-1]))
# atcoder https://atcoder.jp/contests/dp/tasks/dp_b
def main():
    N, K = list(map(int, input().split()))
    h = list(map(int, input().split()))
    dp = [0] * N
    dp[0] = 0
    for i in range(1, N):
        dp[i] = min(dp[k] + abs(h[i] - h[k]) for k in range(max(i - K, 0), i))
    print((dp[N - 1]))


main()

def INT():
    return int(eval(input()))


def MAP():
    return list(map(int, input().split()))


def LIST():
    return list(map(int, input().split()))


N, K = MAP()
h = LIST()
INF = 10**23
dp = [INF] * N
dp[0] = 0
for i in range(1, N):
    for j in range(1, K + 1):
        if i - j < 0:
            continue
        dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j]))
print((dp[-1]))

-# atcoder https://atcoder.jp/contests/dp/tasks/dp_b -def main(): -    N, K = list(map(int, input().split())) -    h = list(map(int, input().split())) -    dp = [0] * N -    dp[0] = 0 -    for i in range(1, N): -        dp[i] = min(dp[k] + abs(h[i] - h[k]) for k in range(max(i - K, 0), i)) -    print((dp[N - 1])) +def INT(): +    return int(eval(input())) -main() +def MAP(): +    return list(map(int, input().split())) + + +def LIST(): +    return list(map(int, input().split())) + + +N, K = MAP() +h = LIST() +INF = 10**23 +dp = [INF] * N +dp[0] = 0 +for i in range(1, N): +    for j in range(1, K + 1): +        if i - j < 0: +            continue +        dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j])) +print((dp[-1]))