#include <bits/stdc++.h>
                using namespace std;
                #define rep(i,k,n) for(int i = k; i < n; i++)  
                #define repp(i,k,n) for(int i = k; i <= n; i++) 
                #define per(i,k,n) for(int i = k; i >= n; i--)

                #define int long long
                #define all(a) a.begin(),a.end()       
                

                #define pb push_back
                typedef long long ll; 
                typedef pair<int, int> ii;
                typedef pair<int,ii > iii; 
                typedef vector<ii> vii;
                typedef vector<char> vchar;
                typedef vector<int> vi;
                #define ff first
                #define ss second

                #define mem(a, b) memset(a, b, sizeof(a));
                #define mem0(a) memset(a, 0, sizeof(a));

                #define INF 1000000000
                //#define mod 1000000007


                /*=\=/=\=/=\=/=\=/=\=/=\=/=\=/=\=/=\=/=\=/=\=/=\=/=\=/=\=/=\=/=\=/=\=/=\=/=\=/=\=/=\=/=*/


                #define endl "\n"
                #define make_graph(m) repp(i, 1, m){ll a, b; cin >> a >> b; gr[a].pb(b), gr[b].pb(a);}
                #define make(a,n) vector<long long> a; rep(i,0,n){ long long temp; cin >> temp; a.push_back(temp);}
                #define printall(c,n) for(int i=0; i< n;i++)cout << c[i] << " ";
                

                /*=\=/=\=/=\=/=\=/=\=/=\=/=\=/=\=/=\=/=\=/=\=/=\=/=\=/=\=/=\=/=\=/=\=/=\=/=\=/=\=/=\=/=*/
           
           
                //int parent[1000000];
                //int asize[1000000];

            /*    int lead (int n){
                    if(parent[n]==n)return n;
                    parent[n] = lead(parent[n]);
                    return parent[n];
                }

                void join (int a, int b){
                    a=lead(a);
                    b=lead(b);
                    
                    if(a!=b){
                        if(a<b)swap(a,b);
                        parent[b] = a;
                        asize[a]+=asize[b];
                    }
                    
                    return;
                }
            */

            /*  int mod;

                int modPow(int a, int b){
                    if(b%2==0){
                        int x = a*a;
                        x%=mod;
                        return modPow(x,b/2);
                    }
                    return (a*modPow(a,b-1))%mod;
                }
            */

                const int MAXN=1000005;
                int a[MAXN];

                int dp[MAXN];
                
                int fun(int n, int k){
                    if(dp[n]!=-1)return dp[n];
                    if(n==0)return 0;
                    if(true){
                        int ans=LLONG_MAX;
                        rep(i,1,min(n+1,k+1)){
                            ans = min(ans, abs(a[n]-a[n-i])+ fun(n-i, k));
                        }
                        dp[n] = ans;
                        return dp[n];
                    }
                    //dp[n] = min(fun(n-1)+abs(a[n]-a[n-1]),fun(n-2)+abs(a[n]-a[n-2]));
                   // return dp[n];
                }
           

                signed main(){
                    ios_base::sync_with_stdio(false);
                    cin.tie(0);
                    cout.tie(0);
                    //cout << fixed;
                    //cout <<setprecision(2);
                    
                    int test=1;

                    //cin >> test;

                    while(test--)
                    {
                        int n,k;
                        cin >> n >> k;
                        rep(i,0,n){
                            cin >> a[i];
                            dp[i]=-1;
                        }
                        cout << fun(n-1, k)<<endl;
                        

                    
                    

                    }
                }

Measured memory_bytes: 11392