#include <bits/stdc++.h>
#define ll long long 
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
using namespace std ; 
 
void swap(int *a ,int *b){
	int temp=*a ; 
	*a=*b ; 
	*b=temp ; 
}
 
string mi(string s ,string t){
    if(s.size()>t.size()){
        return t ; 
    }else if(s.size()<t.size()){
        return s ; 
    }else {
        if(s.compare(t)>=0){
            return t ;
        }else 
        return s ; 
    }
}
 
int isprime(ll n){
    if(n < 2)
        return 0;
    if(n < 4)
        return 1;
    if(n % 2 == 0 or n % 3 == 0)
        return 0;
    for(ll i = 5; i*i <= n; i += 6)
        if(n % i == 0 or n % (i+2) == 0)
            return 0;
    return 1;
}
void pairsort(int a[], int b[], int n){
    pair<int, int> pairt[n];
    for (int i = 0; i < n; i++) 
    {
        pairt[i].first = a[i];
        pairt[i].second = b[i];
    }
    sort(pairt, pairt + n);
    for (int i = 0; i < n; i++) 
    {
        a[i] = pairt[i].first;
        b[i] = pairt[i].second;
    }
}
ll gcd(ll a, ll b){
    if (b == 0)
        return a;
    return gcd(b, a % b); 
}
 
ll fact(ll a){
    ll p=1 ; 
    
    for(int i=1;i<=a;i++)
        p*=i ; 
        
    return p ; 
}
string findsum(string str1, string str2) 
{ 
    // Before proceeding further, make sure length 
    // of str2 is larger. 
    if (str1.length() > str2.length()) 
        swap(str1, str2); 
  
    // Take an empty string for storing result 
    string str = ""; 
  
    // Calculate length of both string 
    int n1 = str1.length(), n2 = str2.length(); 
    int diff = n2 - n1; 
  
    // Initially take carry zero 
    int carry = 0; 
  
    // Traverse from end of both strings 
    for (int i=n1-1; i>=0; i--) 
    { 
        // Do school mathematics, compute sum of 
        // current digits and carry 
        int sum = ((str1[i]-'0') + 
                   (str2[i+diff]-'0') + 
                   carry); 
        str.push_back(sum%10 + '0'); 
        carry = sum/10; 
    } 
  
    // Add remaining digits of str2[] 
    for (int i=n2-n1-1; i>=0; i--) 
    { 
        int sum = ((str2[i]-'0')+carry); 
        str.push_back(sum%10 + '0'); 
        carry = sum/10; 
    } 
  
    // Add remaining carry 
    if (carry) 
        str.push_back(carry+'0'); 
  
    // reverse resultant string 
    reverse(str.begin(), str.end()); 
  
    return str; 
} 
long long bw(long long a, long long b, long long m) {
    a %= m;
    long long res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a % m;
        a = a * a % m;
        b >>= 1;
    }
    return res;
}
 
vector <int> decToBinary(int n) 
{   vector <int> v ; 
 
    for (int i = 31; i >= 0; i--) { 
        int k = n >> i; 
        if (k & 1) 
            v.push_back((int)pow(2,i)) ; 
    } 
    
    return v ; 
} 
 
int bs(int a[],int  n,int x){
    int l=0 ; 
    int r=n-1 ; 
    int mid=(l+r)/2 ;
    while(l<=r){
         mid=l+(r-l)/2 ; 
        
        if(a[mid]==x)
         l =mid+1 ;
        
        else if(a[mid]>x){
            r=mid-1 ; 
        }else
        l=mid+1 ; 
    }
    
    return mid ;
}
 
ll power(ll x, ll y) {
    ll res = 1;
    while (y > 0)
    {
        if (y & 1)
            res = res * x;
 
 
        y = y>>1;
        x = x * x;
    }
    return res;
}
 
 
int main()
{    IOS ;   
    
    int n,k ; 
    cin>>n>>k ;
    int a[n] ; 
    
    for(int i=0;i<n;i++){
        cin>>a[i] ; 
    }
    int dp[n+1] ; 
    // memset(dp,0,sizeof(dp)) ; 
    
    for(int i=0;i<=n;i++){
        dp[i]=INT_MAX ; 
    }
    
    dp[0]=0 ; 
    for(int i=1;i<n;i++){
        for(int j=1;j<=k;j++){
            if(i-j>=0)
              dp[i]=min(dp[i],dp[i-j]+(int)fabs(a[i-j]-a[i])) ; 
        }
    }
    
    // for(int i=0;i<n;i++)
    // cout<<dp[i]<<endl ; 
    
    cout<<dp[n-1] ; 
    return 0;
}



















Measured memory_bytes: 1024