// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace         std;

#define                 int long long

#define                 pb push_back

#define                 mp make_pair

#define st(arr,a)       memset(arr,a,sizeof arr)

#define                 nl cout<<endl

#define                 real signed

#define                 endl '\n'              

#define bitcount(x)     __builtin_popcountll(x)

const int MOD = 1000000007ll;

#define forn(i,a,b)     for(int i=a;i<=b;i++)

#define rfor(i,a,b)     for(int i=a;i>=b;i--)



int arr[100000+50000],n,k;



int dp[150000];



int ans(int ind)

{

    if(ind==n)return 0;

    if(ind>n)return MOD*MOD;



    if(dp[ind]>-1)return dp[ind];





//    dp[ind]=min(abs(arr[ind]-arr[ind+1])+ans(ind+1),abs(arr[ind]-arr[ind+2])+ans(ind+2));

  dp[ind]=MOD*MOD;



  forn(i,ind+1,ind+k)

        dp[ind]=min(dp[ind],abs(arr[ind]-arr[i])+ans(i));

    return dp[ind];

}



signed main() 

{

	ios_base::sync_with_stdio(false);

    cin.tie(NULL);

    cin>>n>>k;

    st(dp,-1);

    st(arr,MOD);

    forn(i,1,n)cin>>arr[i];



    cout<<ans(1);



    



     

     









     return 0;



}



//       g++ -g c2.cpp ; ./a.exe

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

using namespace         std;

#define                 int long long

#define                 pb push_back

#define                 mp make_pair

#define st(arr,a)       memset(arr,a,sizeof arr)

#define                 nl cout<<endl

#define                 real signed

#define                 endl '\n'              

#define bitcount(x)     __builtin_popcountll(x)

const int MOD = 1000000007ll;

#define forn(i,a,b)     for(int i=a;i<=b;i++)

#define rfor(i,a,b)     for(int i=a;i>=b;i--)

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

#define pi pair<int,int>

#define X first

#define Y second

#define N 1014159

#define  vi vector<int>

#define  v  vector





int n,dp[N],h[N];



int k;

void solve()

{

    cin>>n>>k;

    forn(i,1,n)cin>>h[i];

    

    dp[1] = 0;

    

    forn(i,2,n)

    {

        dp[i] = MOD*MOD;

        forn(j,max(1LL,i-k),i-1)

        {

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

        }

    }

    cout<<dp[n];

    

}





signed main() 

{

    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); 

    int t = 1;

    //cin>>t;

    while(t--)

    {

        solve();

    }







    return 0;



}



//       g++ -g c.cpp ; ./a.exe

// 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;





}