def main():

    n, k = list(map(int, input().split()))

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

    dp = [None]*n

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

    for i, hi in enumerate(h[2:], 2):

        di = 10**9

        m = max(0, i-k)

        for dj, hj in zip(dp[m:i], h[m:i]):

            hh = hj-hi

            if hh>0:

                d = dj + hh

            else:

                d = dj - hh

            if di > d:

                di = d

        dp[i] = di

    print((dp[-1]))



if __name__ == "__main__":

    main()
n, k = list(map(int, input().split()))

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

dp = [10**9+1]*n

dp[0] = 0

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

for i in range(2, n):

    for j in range(1, min(i+1, k+1)):

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

print((dp[n-1]))

    
def main():
    n, k = list(map(int, input().split()))
    h = list(map(int, input().split()))
    dp = [None] * n
    dp[0], dp[1] = 0, abs(h[0] - h[1])
    for i, hi in enumerate(h[2:], 2):
        di = 10**9
        m = max(0, i - k)
        for dj, hj in zip(dp[m:i], h[m:i]):
            hh = hj - hi
            if hh > 0:
                d = dj + hh
            else:
                d = dj - hh
            if di > d:
                di = d
        dp[i] = di
    print((dp[-1]))


if __name__ == "__main__":
    main()

n, k = list(map(int, input().split()))
h = list(map(int, input().split()))
dp = [10**9 + 1] * n
dp[0] = 0
dp[1] = abs(h[1] - h[0])
for i in range(2, n):
    for j in range(1, min(i + 1, k + 1)):
        dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j]))
print((dp[n - 1]))

-def main(): -    n, k = list(map(int, input().split())) -    h = list(map(int, input().split())) -    dp = [None] * n -    dp[0], dp[1] = 0, abs(h[0] - h[1]) -    for i, hi in enumerate(h[2:], 2): -        di = 10**9 -        m = max(0, i - k) -        for dj, hj in zip(dp[m:i], h[m:i]): -            hh = hj - hi -            if hh > 0: -                d = dj + hh -            else: -                d = dj - hh -            if di > d: -                di = d -        dp[i] = di -    print((dp[-1])) - - -if __name__ == "__main__": -    main() +n, k = list(map(int, input().split())) +h = list(map(int, input().split())) +dp = [10**9 + 1] * n +dp[0] = 0 +dp[1] = abs(h[1] - h[0]) +for i in range(2, n): +    for j in range(1, min(i + 1, k + 1)): +        dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j])) +print((dp[n - 1]))