# import sys

# sys.setrecursionlimit(10**5)

# from collections import defaultdict

geta = lambda fn: list(map(fn, input().split()))

gete = lambda fn: fn(eval(input()))



def main():

    N, K = geta(int)

    h = geta(int)



    INF = 1 << 60

    dp = [INF]*N

    dp[0], dp[1] = 0, abs(h[0] - h[1])



    for i in range(2, N):

        lb = max(0, i-K)

        dp[i] = min((dp[j] + abs(h[i]-h[j]) for j in range(lb,i)))



    print((dp[-1]))



if __name__ == "__main__":

    main()
# import sys

# sys.setrecursionlimit(10**5)

# from collections import defaultdict

geta = lambda fn: list(map(fn, input().split()))

gete = lambda fn: fn(eval(input()))



def main():

    N, K = geta(int)

    h = geta(int)



    INF = 10 << 60

    dp = [INF]*N

    dp[0], dp[1] = 0, abs(h[0] - h[1])



    for i in range(2, N):

        lb = max(0, i-K)

        tmp, hi = INF, h[i]

        for dpj, hj in zip(dp[lb:i], h[lb:i]):

            tmp = min(tmp, dpj + abs(hi-hj))

        dp[i] = tmp



    print((dp[-1]))



if __name__ == "__main__":

    main()
# import sys
# sys.setrecursionlimit(10**5)
# from collections import defaultdict
geta = lambda fn: list(map(fn, input().split()))
gete = lambda fn: fn(eval(input()))


def main():
    N, K = geta(int)
    h = geta(int)
    INF = 1 << 60
    dp = [INF] * N
    dp[0], dp[1] = 0, abs(h[0] - h[1])
    for i in range(2, N):
        lb = max(0, i - K)
        dp[i] = min((dp[j] + abs(h[i] - h[j]) for j in range(lb, i)))
    print((dp[-1]))


if __name__ == "__main__":
    main()

# import sys
# sys.setrecursionlimit(10**5)
# from collections import defaultdict
geta = lambda fn: list(map(fn, input().split()))
gete = lambda fn: fn(eval(input()))


def main():
    N, K = geta(int)
    h = geta(int)
    INF = 10 << 60
    dp = [INF] * N
    dp[0], dp[1] = 0, abs(h[0] - h[1])
    for i in range(2, N):
        lb = max(0, i - K)
        tmp, hi = INF, h[i]
        for dpj, hj in zip(dp[lb:i], h[lb:i]):
            tmp = min(tmp, dpj + abs(hi - hj))
        dp[i] = tmp
    print((dp[-1]))


if __name__ == "__main__":
    main()

-    INF = 1 << 60 +    INF = 10 << 60 -        dp[i] = min((dp[j] + abs(h[i] - h[j]) for j in range(lb, i))) +        tmp, hi = INF, h[i] +        for dpj, hj in zip(dp[lb:i], h[lb:i]): +            tmp = min(tmp, dpj + abs(hi - hj)) +        dp[i] = tmp