if __name__=='__main__':

    n,k,*h=list(map(int,open(0).read().split()))

    dp=[0]

    for i,t in enumerate(h):

        if i:

            j=max(0,i-k)

            dp.append(min(v+abs(t-w)for v,w in zip(dp[j:i],h[j:i])))

    print((dp[-1]))
n,k,*h=list(map(int,open(0).read().split()));d=[0]*n

for i in range(1,n):j=max(0,i-k);d[i]=(min(v+abs(h[i]-w)for v,w in zip(d[j:i],h[j:i])))

print((d[-1]))
if __name__ == "__main__":
    n, k, *h = list(map(int, open(0).read().split()))
    dp = [0]
    for i, t in enumerate(h):
        if i:
            j = max(0, i - k)
            dp.append(min(v + abs(t - w) for v, w in zip(dp[j:i], h[j:i])))
    print((dp[-1]))

n, k, *h = list(map(int, open(0).read().split()))
d = [0] * n
for i in range(1, n):
    j = max(0, i - k)
    d[i] = min(v + abs(h[i] - w) for v, w in zip(d[j:i], h[j:i]))
print((d[-1]))

-if __name__ == "__main__": -    n, k, *h = list(map(int, open(0).read().split())) -    dp = [0] -    for i, t in enumerate(h): -        if i: -            j = max(0, i - k) -            dp.append(min(v + abs(t - w) for v, w in zip(dp[j:i], h[j:i]))) -    print((dp[-1])) +n, k, *h = list(map(int, open(0).read().split())) +d = [0] * n +for i in range(1, n): +    j = max(0, i - k) +    d[i] = min(v + abs(h[i] - w) for v, w in zip(d[j:i], h[j:i])) +print((d[-1]))