using System;
using System.Linq;

namespace AlgoSample
{
    class Program
    {
        static void Main(string[] args)
        {
            //Frod();
            Frog2();
        }

        public static void Frog2()
        {
            String[] input = Console.ReadLine().Split(' ');
            int N = int.Parse(input[0]);
            int K = int.Parse(input[1]);
            int[] h = new int[N];
            h = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
            // dp[n] : minimum cost to reach to place n+1
            int[] dp = new int[N];
            // init
            dp[0] = 0; // Cost for 1
            for(int i=1; i<N; i++)
            {
                for (int j = 1; j <= i; j++)
                {
                    if(j == 1)
                    {
                        dp[i] = dp[i - 1] + Math.Abs(h[i] - h[i - j]);
                    }
                    if(j > K)
                    {
                        break;
                    }
                    else
                    {
                        int calcedCost = dp[i - j] + Math.Abs(h[i] - h[i - j]);
                        if (calcedCost < dp[i])
                        {
                            dp[i] = calcedCost;
                        }
                    }
                }
            }
            Console.WriteLine(dp[N - 1]);
        }

        public static void Frod()
        {
            int N = int.Parse(Console.ReadLine());
            int[] h = new int[N];
            h = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
            // dp[n] : minimum cost to reach to place n+1
            int[] dp = new int[N];
            // init
            dp[0] = 0; // Cost for 1
            dp[1] = Math.Abs(h[1] - h[0]); // Cost for 2

            for (int i = 2; i < N; i++)
            {
                dp[i] = Math.Min(Math.Abs(h[i] - h[i - 1]) + dp[i-1], Math.Abs(h[i] - h[i - 2]) + dp[i - 2]);
            }
            Console.WriteLine(dp[N - 1]);
        }

        public static void Otoshidama()
        {
            String[] inputs = Console.ReadLine().Split(' ');
            int N = int.Parse(inputs[0]);
            int Y = int.Parse(inputs[1]);
            int y = Y / 1000;
            Solve(N, y);
        }

        public static void Solve(int n, int y)
        {
            bool[,] dp = new bool[n+1, y+1];
            // init: for n=0, only when y=0 is true, otherwise false
            dp[0, 0] = true;
            int[,][] dp2= new int[n+1, y+1][];
            dp2[0, 0] = new int[3] { 0, 0, 0 };
            for(int i=1; i<=n; i++)
            {
                for(int j=1; j<=y; j++)
                {
                    if(j < 5)
                    {
                        if(dp[i-1, j-1])
                        {
                            dp2[i, j] = new int[3] { dp2[i - 1, j - 1][0] + 1, dp2[i - 1, j - 1][1], dp2[i - 1, j - 1][2] };
                        }
                        dp[i, j] = dp[i - 1, j - 1];
                    }
                    else if(j < 10)
                    {
                        if (dp[i - 1, j - 1])
                        {
                            dp2[i, j] = new int[3] { dp2[i - 1, j - 1][0] + 1, dp2[i - 1, j - 1][1], dp2[i - 1, j - 1][2] };
                        }
                        else if(dp[i-1, j-5])
                        {
                            dp2[i, j] = new int[3] { dp2[i - 1, j - 5][0], dp2[i - 1, j - 5][1] + 1, dp2[i - 1, j - 5][2] };
                        }
                        dp[i, j] = dp[i - 1, j - 1] || dp[i - 1, j - 5];
                    }
                    else
                    {
                        if (dp[i - 1, j - 1])
                        {
                            dp2[i, j] = new int[3] { dp2[i - 1, j - 1][0] + 1, dp2[i - 1, j - 1][1], dp2[i - 1, j - 1][2] };
                        }
                        else if (dp[i - 1, j - 5])
                        {
                            dp2[i, j] = new int[3] { dp2[i - 1, j - 5][0], dp2[i - 1, j - 5][1] + 1, dp2[i - 1, j - 5][2] };
                        }
                        else if(dp[i - 1, j - 10])
                        {
                            dp2[i, j] = new int[3] { dp2[i - 1, j - 10][0], dp2[i - 1, j - 10][1], dp2[i - 1, j - 10][2]+1 };
                        }
                        dp[i, j] = dp[i - 1, j - 1] || dp[i - 1, j - 5] || dp[i - 1, j - 10];
                    }
                }
            }
            if(dp[n, y])
            {
                Console.WriteLine(dp2[n, y][2] + " " + dp2[n, y][1] + " " + dp2[n, y][0]);
            }
            else
            {
                Console.WriteLine("-1 -1 -1");
            }
        }
    }

    public class nCk
    {
        private static nCk _ins;
        private static long[,] _table;
        private readonly int INT_MAX = 63245;
        private nCk()
        {
            _table = new long[INT_MAX, INT_MAX];
            // init
            for(int i=0; i<INT_MAX; i++)
            {
                _table[i,0] = 1;
            }
            for(int i=1; i<INT_MAX; i++)
            {
                for(int j=1; j<=i; j++)
                {
                    _table[i, j] = i  * _table[i - 1, j - 1] / j;
                }
            }
        }

        public static nCk GetInstance()
        {
            if(_ins == null)
            {
                return new nCk();
            }
            else
            {
                return _ins;
            }
        }

        public long calcNCK(int n, int k)
        {
            return _table[n, k];
        }
    }

}


Measured memory_bytes: 20448