def solve():

  n,k=[int(i) for i in input().split()]

  h=[int(i) for i in input().split()]

  dp=[0]*n

  for i, hi in enumerate(h):

    if i==0: continue

    dp[i]=min([dp[j] + abs(hi-h[j]) for j in range(max(i-k,0),i)])

  return dp[n-1]

print((solve()))

def solve():

  n,k=[int(i) for i in input().split()]

  h=[int(i) for i in input().split()]

  dp=[0]*n

  for i, hi in enumerate(h):

    if i==0: continue

    x = max(0, i - k)

    dp[i]=min([dpj + abs(hi-hj) for dpj, hj in zip(dp[x:i], h[x:i])])

    #スライサー[x:i]で[x]～[i-1]の部分リストを取得

  return dp[n-1]

print((solve()))

def solve():
    n, k = [int(i) for i in input().split()]
    h = [int(i) for i in input().split()]
    dp = [0] * n
    for i, hi in enumerate(h):
        if i == 0:
            continue
        dp[i] = min([dp[j] + abs(hi - h[j]) for j in range(max(i - k, 0), i)])
    return dp[n - 1]


print((solve()))

def solve():
    n, k = [int(i) for i in input().split()]
    h = [int(i) for i in input().split()]
    dp = [0] * n
    for i, hi in enumerate(h):
        if i == 0:
            continue
        x = max(0, i - k)
        dp[i] = min([dpj + abs(hi - hj) for dpj, hj in zip(dp[x:i], h[x:i])])
        # スライサー[x:i]で[x]～[i-1]の部分リストを取得
    return dp[n - 1]


print((solve()))

-        dp[i] = min([dp[j] + abs(hi - h[j]) for j in range(max(i - k, 0), i)]) +        x = max(0, i - k) +        dp[i] = min([dpj + abs(hi - hj) for dpj, hj in zip(dp[x:i], h[x:i])]) +        # スライサー[x:i]で[x]～[i-1]の部分リストを取得