def woo(x, y):

    res = [0]*x

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

    for i in range(1, x):

        res[i] = min(abs(z[i] - z[j]) + res[j] for j in range(max(0, i - y), i))

    return res



if __name__ == '__main__':

    x, y = list(map(int, input().split()))

    r = woo(x, y)

    f = len(r)-1

    print((r[f]))



def main():

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

    arr = [int(e) for e in input().split()]

    t = [float('inf')] * (n + 1)

    t[0] = 0

    t[1] = 0 

    for i in range(2, n + 1):

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

            if i - j <= 0:

                break

            l1 = t[i - j] + abs(arr[i - j - 1] - arr[i - 1])

            t[i] = min(t[i], l1)

    print((t[n]))

    return 0





if __name__ == '__main__':

    main()
def woo(x, y):
    res = [0] * x
    z = list(map(int, input().split()))
    for i in range(1, x):
        res[i] = min(abs(z[i] - z[j]) + res[j] for j in range(max(0, i - y), i))
    return res


if __name__ == "__main__":
    x, y = list(map(int, input().split()))
    r = woo(x, y)
    f = len(r) - 1
    print((r[f]))

def main():
    n, k = tuple([int(e) for e in input().split()])
    arr = [int(e) for e in input().split()]
    t = [float("inf")] * (n + 1)
    t[0] = 0
    t[1] = 0
    for i in range(2, n + 1):
        for j in range(1, k + 1):
            if i - j <= 0:
                break
            l1 = t[i - j] + abs(arr[i - j - 1] - arr[i - 1])
            t[i] = min(t[i], l1)
    print((t[n]))
    return 0


if __name__ == "__main__":
    main()

-def woo(x, y): -    res = [0] * x -    z = list(map(int, input().split())) -    for i in range(1, x): -        res[i] = min(abs(z[i] - z[j]) + res[j] for j in range(max(0, i - y), i)) -    return res +def main(): +    n, k = tuple([int(e) for e in input().split()]) +    arr = [int(e) for e in input().split()] +    t = [float("inf")] * (n + 1) +    t[0] = 0 +    t[1] = 0 +    for i in range(2, n + 1): +        for j in range(1, k + 1): +            if i - j <= 0: +                break +            l1 = t[i - j] + abs(arr[i - j - 1] - arr[i - 1]) +            t[i] = min(t[i], l1) +    print((t[n])) +    return 0 -    x, y = list(map(int, input().split())) -    r = woo(x, y) -    f = len(r) - 1 -    print((r[f])) +    main()