def main():

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

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

    m = [0] * n

    m[0] = 0

    for i in range(1, n):

        s = max(0, i-k)

        m[i] = min([ml + abs(hl - h[i]) for ml, hl in zip(m[s:i], h[s:i])])

    print((m[-1]))



main()

def main():

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

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

    m = [0] * n

    m[0] = 0

    for i, hi in enumerate(h):

        if i == 0:

            continue

        s = max(0, i-k)

        m[i] = min([ml + abs(hl - hi) for ml, hl in zip(m[s:i], h[s:i])])

    print((m[-1]))



main()

def main():
    n, k = list(map(int, input().split()))
    h = list(map(int, input().split()))
    m = [0] * n
    m[0] = 0
    for i in range(1, n):
        s = max(0, i - k)
        m[i] = min([ml + abs(hl - h[i]) for ml, hl in zip(m[s:i], h[s:i])])
    print((m[-1]))


main()

def main():
    n, k = list(map(int, input().split()))
    h = list(map(int, input().split()))
    m = [0] * n
    m[0] = 0
    for i, hi in enumerate(h):
        if i == 0:
            continue
        s = max(0, i - k)
        m[i] = min([ml + abs(hl - hi) for ml, hl in zip(m[s:i], h[s:i])])
    print((m[-1]))


main()

-    for i in range(1, n): +    for i, hi in enumerate(h): +        if i == 0: +            continue -        m[i] = min([ml + abs(hl - h[i]) for ml, hl in zip(m[s:i], h[s:i])]) +        m[i] = min([ml + abs(hl - hi) for ml, hl in zip(m[s:i], h[s:i])])