#include<bits/stdc++.h>
#include <vector>
#include <string>
#include <cmath>
#include <set>
#include <stdio.h>
#include <algorithm>
#include <map>
#include <queue>
#include <unordered_map>
#define ll long long
#define pii pair <int, int>
#define pll pair <long long, long long>
#define plxy pair <long long, pll>
#define INT_MAX 1e18
#define Keep_calm_You_are_still_learning ios_base::sync_with_stdio(false);cin.tie(NULL);
using namespace std;
using namespace std;
const int mod=1e9+7;
const int N=1e5+5;
ll n,k,arr[N];
ll dp[N];
ll solve(ll idx)
{
    if(idx==n-1)
        return 0;
    if(dp[idx]!=-1)
        return dp[idx];
    ll choice=INT_MAX;
    for(ll i=1;i<=k;i++)
    {
        if(idx+i<n)
            choice=min(choice,solve(idx+i)+abs(arr[idx+i]-arr[idx]));
    }
    dp[idx]=choice;
    return dp[idx];
}
int main()
{
     Keep_calm_You_are_still_learning
     cin>>n>>k;
     for(ll i=0;i<n;i++)
     {
         cin>>arr[i];
     }
     memset(dp,-1,sizeof(dp));
     cout<<solve(0);
    return 0;
}
