def main():

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

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

    a = [0] * n

    a[0] = 0

    for i in range(1, n):

        hi = h[i]

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

    print((a[-1]))



if __name__ == '__main__':

    main()

def main():

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

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

    a = [0] * n

    a[0] = 0

    for i in range(1, n):

        hi = h[i]

        s = max(0, i - k)

        a[i] = min(aj + abs(hi - hj) for hj, aj in zip(h[s:i], a[s:i]))

    print((a[-1]))



if __name__ == '__main__':

    main()

def main():
    n, k = list(map(int, input().split()))
    h = list(map(int, input().split()))
    a = [0] * n
    a[0] = 0
    for i in range(1, n):
        hi = h[i]
        a[i] = min(a[j] + abs(hi - h[j]) for j in range(max(0, i - k), i))
    print((a[-1]))


if __name__ == "__main__":
    main()

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


if __name__ == "__main__":
    main()

-        a[i] = min(a[j] + abs(hi - h[j]) for j in range(max(0, i - k), i)) +        s = max(0, i - k) +        a[i] = min(aj + abs(hi - hj) for hj, aj in zip(h[s:i], a[s:i]))