# dpB - Frog 2

# 貰うDP

def main():

    n, k = tuple(map(int, input().rstrip().split()))

    A = tuple(map(int, input().rstrip().split()))

    dp = [float("inf")] * n

    dp[0] = 0

    for i in range(1, n):

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

            if i - j >= 0:

                dp[i] = min(dp[i], dp[i - j] + abs(A[i] - A[i - j]))

    print((dp[-1]))





if __name__ == "__main__":

    main()
# dpB - Frog 2

# 配るDP

def main():

    n, k = tuple(map(int, input().rstrip().split()))

    A = tuple(map(int, input().rstrip().split()))

    dp = [float("inf")] * n

    dp[0] = 0

    for i in range(n):

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

            if i + j < n:

                dp[i + j] = min(dp[i + j], dp[i] + abs(A[i] - A[i + j]))

    print((dp[-1]))





if __name__ == "__main__":

    main()
# dpB - Frog 2
# 貰うDP
def main():
    n, k = tuple(map(int, input().rstrip().split()))
    A = tuple(map(int, input().rstrip().split()))
    dp = [float("inf")] * n
    dp[0] = 0
    for i in range(1, n):
        for j in range(1, k + 1):
            if i - j >= 0:
                dp[i] = min(dp[i], dp[i - j] + abs(A[i] - A[i - j]))
    print((dp[-1]))


if __name__ == "__main__":
    main()

# dpB - Frog 2
# 配るDP
def main():
    n, k = tuple(map(int, input().rstrip().split()))
    A = tuple(map(int, input().rstrip().split()))
    dp = [float("inf")] * n
    dp[0] = 0
    for i in range(n):
        for j in range(1, k + 1):
            if i + j < n:
                dp[i + j] = min(dp[i + j], dp[i] + abs(A[i] - A[i + j]))
    print((dp[-1]))


if __name__ == "__main__":
    main()

-# 貰うDP +# 配るDP -    for i in range(1, n): +    for i in range(n): -            if i - j >= 0: -                dp[i] = min(dp[i], dp[i - j] + abs(A[i] - A[i - j])) +            if i + j < n: +                dp[i + j] = min(dp[i + j], dp[i] + abs(A[i] - A[i + j]))