# -*- coding: utf-8 -*-

"""

B - Frog 2

https://atcoder.jp/contests/dp/tasks/dp_b



"""

import sys

from sys import stdin





def solve(ashiba, N, K):

    dp = [float('inf')] * N

    dp[0] = 0

    for i in range(1, len(ashiba)):

        paths = [dp[j]+abs(ashiba[j]-ashiba[i]) for j in range(max(0, i-K), i)]

        dp[i] = min(paths)

    return dp[-1]





def main(args):

    N, K = list(map(int, input().split()))

    ashiba = [int(i) for i in input().split()]

    ans = solve(ashiba, N, K)

    print(ans)





if __name__ == '__main__':

    main(sys.argv[1:])

# -*- coding: utf-8 -*-

"""

B - Frog 2

https://atcoder.jp/contests/dp/tasks/dp_b

AC

"""

import sys

from sys import stdin





def solve(ashiba, N, K):

    dp = [float('inf')] * N

    dp[0] = 0

    for i in range(1, len(ashiba)):

        paths = [dp[j]+abs(ashiba[j]-ashiba[i]) for j in range(max(0, i-K), i)]

        dp[i] = min(paths)

    return dp[-1]





def main(args):

    N, K = list(map(int, input().split()))

    ashiba = [int(i) for i in input().split()]

    ans = solve(ashiba, N, K)

    print(ans)





if __name__ == '__main__':

    main(sys.argv[1:])

# -*- coding: utf-8 -*-
"""
B - Frog 2
https://atcoder.jp/contests/dp/tasks/dp_b
"""
import sys
from sys import stdin


def solve(ashiba, N, K):
    dp = [float("inf")] * N
    dp[0] = 0
    for i in range(1, len(ashiba)):
        paths = [dp[j] + abs(ashiba[j] - ashiba[i]) for j in range(max(0, i - K), i)]
        dp[i] = min(paths)
    return dp[-1]


def main(args):
    N, K = list(map(int, input().split()))
    ashiba = [int(i) for i in input().split()]
    ans = solve(ashiba, N, K)
    print(ans)


if __name__ == "__main__":
    main(sys.argv[1:])

# -*- coding: utf-8 -*-
"""
B - Frog 2
https://atcoder.jp/contests/dp/tasks/dp_b
AC
"""
import sys
from sys import stdin


def solve(ashiba, N, K):
    dp = [float("inf")] * N
    dp[0] = 0
    for i in range(1, len(ashiba)):
        paths = [dp[j] + abs(ashiba[j] - ashiba[i]) for j in range(max(0, i - K), i)]
        dp[i] = min(paths)
    return dp[-1]


def main(args):
    N, K = list(map(int, input().split()))
    ashiba = [int(i) for i in input().split()]
    ans = solve(ashiba, N, K)
    print(ans)


if __name__ == "__main__":
    main(sys.argv[1:])

+AC