// Original C++ solution (slower):


#include<bits/stdc++.h>

#define lld         long long int

#define llf         long double

#define pb          push_back

#define mp          make_pair

#define IN          insert

#define loopN(n)    for(lld i=0;i<n;i++)

#define loop(p,n)   for(lld i=p;i<=n;i++)

#define rloop(p,n)  for(lld i=n;i>=p;i--)

#define loop2(p,n)  for(lld j=p;j<=n;j++)

#define all(v)      v.begin(),v.end()

#define F1          first

#define F2          second

#define INF         2e18

#define FILE        freopen("input.txt","r",stdin);freopen("output.txt","w",stdout)

#define PI          2*acos(0.0)

#define ISO         ios::sync_with_stdio(false);cin.tie(0)

#define mem(a,b)    memset(a,b,sizeof(a))

#define case(z)     cout<<"Case "<<z<<": ";

using namespace std;

const lld mod=1e9+7;



lld n,k;

lld a[100005];

lld dp[100005];



lld fun(lld p)

{



    if(p>=n-1)

    {

        return 0;

    }

    if(n-1-p<=k)

    {

        return abs(a[p]-a[n-1]);

    }

    if(dp[p]!=-1)

    {

        return dp[p];

    }

    lld ans=INF;

    loop(1,k)

    {

        ans=min(abs(a[p]-a[p+i])+fun(p+i),ans);

    }

    return dp[p]=ans;





}



int main()

{

    ISO;



    //lld t;cin>>t;while(t--)

    mem(dp,-1);

    cin>>n>>k;

    loopN(n)cin>>a[i];

    cout<<fun(0)<<endl;



    return 0;

}




// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

using namespace std;



#define ff              first

#define ss              second

#define ll             long long

#define pb              push_back

#define mp              make_pair

#define pii             pair<ll,ll>

#define vi              vector<ll>

#define mii             map<ll,ll>

#define pqb             priority_queue<ll>

#define pqs             priority_queue<ll,vi,greater<ll> >

#define setbits(x)      __builtin_popcountll(x)

#define zrobits(x)      __builtin_ctzll(x)

#define mod             1000000007

#define inf             1e18

#define ps(x,y)         fixed<<setprecision(y)<<x

#define mk(arr,n,type)  type *arr=new type[n];

#define w(x)            ll x; cin>>x; while(x--)

#define FIO             ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)





ll solve(ll n,vector<ll>v,ll k){

    ll op1,op2;

    vector<ll>dp(n+1);

dp[1]=0;

for(int i=2;i<=n;i++)

{

    dp[i]=INT_MAX;

    for(int j=i-1;j>=1&&(i-j)<=k;j--)

    {

        dp[i]=min(dp[i],abs(v[i]-v[j])+dp[j]);

    }

}



return dp[n];



}



int main()

{

    FIO;

    ll a,b,c,d,e,f=0,sum=0,ans,ck=0,m,n,k,p,q,r,t,add=0,x,y,z;

    string s;



    cin>>n>>k;

    vector<ll>v(n+1);

    for(int i=1;i<=n;i++){cin>>v[i];}

    ans=solve(n,v,k);

    cout<<ans<<endl;







    return 0;

}

// Fastest known solution for this problem:
#include <iostream>

#include <array>

#include <vector>

#include <iomanip>

#include <string>

#include <cmath>

#include <array>

#include <algorithm>

#include <queue>

#include <map>

using namespace std;



typedef  pair<int,int> P;

typedef long long LL;



const LL  INF=1<<31;







int main(){

     int n,k;

     cin>>n>>k;

     int h[100000];

     for(int i=0;i<n;++i){

          cin>>h[i];

     }

     LL dp[100001];



      for(int i=0;i<n;++i){

           dp[i]=INF;

      }

            dp[0]=0;

            dp[1]=abs(h[0]-h[1]);







     for(int i=2;i<n;++i){

          dp[i]=dp[i-1]+abs(h[i-1]-h[i]);

          for(int j=2;j<=k;++j){



               if(j<=i)dp[i]=min(dp[i],dp[i-j]+abs(h[i-j]-h[i]));

          }

     }     



     cout<<dp[n-1]<<endl;





}