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 in range(2, n):

        di = 10**9

        hi = h[i]

        for j in range(max(0, i-k), i):

            dj = dp[j] + abs(h[j]-hi)

            if di > dj:

                di = dj

        dp[i] = di

    print((dp[-1]))



if __name__ == "__main__":

    main()
def main():

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

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

    m = [0 if i<k else i-k for i in range(2, n)]

    dp = [None]*n

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

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

        di = 10**9

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

            d = dj + abs(hj-hi)

            if di > d:

                di = d

        dp[i] = di

    print((dp[-1]))



if __name__ == "__main__":

    main()
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 in range(2, n):
        di = 10**9
        hi = h[i]
        for j in range(max(0, i - k), i):
            dj = dp[j] + abs(h[j] - hi)
            if di > dj:
                di = dj
        dp[i] = di
    print((dp[-1]))


if __name__ == "__main__":
    main()

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


if __name__ == "__main__":
    main()

+    m = [0 if i < k else i - k for i in range(2, n)] -    for i in range(2, n): +    for i, (hi, mi) in enumerate(zip(h[2:], m), 2): -        hi = h[i] -        for j in range(max(0, i - k), i): -            dj = dp[j] + abs(h[j] - hi) -            if di > dj: -                di = dj +        for dj, hj in zip(dp[mi:i], h[mi:i]): +            d = dj + abs(hj - hi) +            if di > d: +                di = d