# 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 = i-K if i > K else 0

        tmp, hi = INF, h[i]

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

            upd = dpj + abs(hi-hj)

            if tmp > upd:

                tmp = upd

        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 = i - K if i > K else 0

        for j in range(lb, i):

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

            if tmp < dp[i]:

                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 = 10 << 60
    dp = [INF] * N
    dp[0], dp[1] = 0, abs(h[0] - h[1])
    for i in range(2, N):
        lb = i - K if i > K else 0
        tmp, hi = INF, h[i]
        for dpj, hj in zip(dp[lb:i], h[lb:i]):
            upd = dpj + abs(hi - hj)
            if tmp > upd:
                tmp = upd
        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 = i - K if i > K else 0
        for j in range(lb, i):
            tmp = dp[j] + abs(h[j] - h[i])
            if tmp < dp[i]:
                dp[i] = tmp
    print((dp[-1]))


if __name__ == "__main__":
    main()

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