#include<bits/stdc++.h>
using namespace std;
#define int long long int 
#define mod 998244353
#define faster ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
//#include<bits/stdc++.h>
bool sortbydesc(int a,int b){
	return a>b;
}
bool sortbydesc1(pair<int,int> a,pair<int,int> b){
	return a.first>b.first;
}
struct triple{
	int n1;

	int n2;
	int n3; 
};

class Graph 
{ 
	int V; 
	list<int> *adj; 
public: 
	Graph(int V); 
	void addEdge(int v, int w);
	void DFSUtil(int v, bool *visited,std::vector<int> &v1); 
}; 

Graph::Graph(int V) 
{ 
	this->V = V; 
	adj = new list<int>[V]; 
} 

void Graph::addEdge(int v, int w) 
{ 
	adj[v].push_back(w);}

void Graph::DFSUtil(int v, bool *visited,vector<int>&v1) 
{ 
	
	visited[v] = true;
	v1.push_back(v); 
	// cout << v << " "; 

	
	list<int>::iterator i; 
	for(i = adj[v].begin(); i != adj[v].end(); ++i) 
		if(!visited[*i]) 
			DFSUtil(*i, visited,v1); 
} 
// ll dp(ll n,ll k,ll *h,ll *a){
// 	if(n>=k){
// 		return 0;
// 	}
// 	else if(n==k-1){
// 		return a[k-1];
// 	}
// 	else if(n==k-2||n==k-3){
// 		return a[k-2];
// 	}
// 	else if(h[n]==-1){
// 		//return max(a[n]+)

// 	}
// }
bool  print1(int a[], int n, int ind) 
{ int k=1;
    // print from ind-th index to (n+i)th index. 
    for (int i = ind; i < n + ind; i++) {
    	if(a[i%n]==k){
    		k++;
    	}
    	else{
    		return 0;
    	}
       // cout << a[(i % n)] << " "; 
    }
    return 1;
}
bool  print2(int *a, int n, int ind) 
{ 
	int k=1;
    // print from ind-th index to (n+i)th index. 
    for (int i = ind; i<n+ ind; i++) {
    	if(a[i%n]==k){
    		k++;
    	}

    	else{
    		return 0;
    	}
        //cout << a[(i % n)] << " "; 
    }
    return 1;
}
int divCount(int n) 
{ 
    // sieve method for prime calculation 
    bool hash[n + 1]; 
    memset(hash, true, sizeof(hash)); 
    for (int p = 2; p * p < n; p++) 
        if (hash[p] == true) 
            for (int i = p * 2; i < n; i += p) 
                hash[i] = false; 
  
    // Traversing through all prime numbers 
    int total = 1; 
    for (int p = 2; p <= n; p++) { 
        if (hash[p]) { 
  
            // calculate number of divisor 
            // with formula total div =  
            // (p1+1) * (p2+1) *.....* (pn+1) 
            // where n = (a1^p1)*(a2^p2)....  
            // *(an^pn) ai being prime divisor 
            // for n and pi are their respective  
            // power in factorization 
            int count = 0; 
            if (n % p == 0) { 
                while (n % p == 0) { 
                    n = n / p; 
                    count++; 
                } 
                total = total * (count + 1); 
            } 
        } 
    } 
    return total; 
} 




int32_t main(){
	int n;
	cin>>n;
	int k;
	cin>>k;
	int a[n];
	for(int i=0;i<n;i++){
		cin>>a[i];
	}
	int dp[n];
	for(int i=0;i<n;i++){
		dp[i]=0;
	}
	dp[0]=0;
	dp[1]=abs(a[1]-a[0]);
	if(k>=2)dp[2]=min(dp[1]+abs(a[2]-a[1]),abs(a[2]-a[0]));
	else dp[2]=dp[1]+abs(a[2]-a[1]);
	for(int i=3;i<n;i++){
		int min1=INT_MAX;
		for(int j=1;j<=k;j++){
			if(i-j>=0)min1=min(min1,dp[i-j]+abs(a[i]-a[i-j]));
			//else min1=min(min1,)


		}
		dp[i]=min1;

		//dp[i]=min(dp[i-2]+abs(a[i-2]-a[i]),dp[i-1]+abs(a[i-1]-a[i]));
	}
	// for(int i=0;i<n;i++){
	// 	cout<<dp[i]<<endl;
	// }
	cout<<dp[n-1]<<endl;

	
	return 0;
}

Measured memory_bytes: 1792