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

#define sf1(n) scanf("%d",&n)

#define sf2(n,q) scanf("%d %d",&n,&q)

#define f first

#define ss second

#define pb push_back

#define lli long long

#define mem(ar,false) memset(ar,false,sizeof(ar))

#define mpr make_pair

#define endl "\n"

#define mod 1000000007

#define inf 9000000000000000000LL

using namespace std;



int ar[200005];

int n,k;

int dp[100005];

int func(int i)

{



    if(i==n-1)

        return 0;

    if(dp[i]!=-1)

        return dp[i];

    int ret = INT_MAX;



    for(int j=1;j<=k;j++)

    {

        if(i+j<n)

            ret = min(ret,abs(ar[i]-ar[i+j])+func(i+j));

    }

    return dp[i] = ret;

}



int main()

{

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

    mem(dp,-1);

    cin >> n >> k;

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

        cin >> ar[i];



     int ans = func(0);

  /*  for(int i=1; i<n; i++)

    {

        int ret1 = dp[i-1]+abs(ar[i]-ar[i-1]);

        int ret2 = INT_MAX;

        if(i-2>=0)

            ret2 = dp[i-2]+abs(ar[i]-ar[i-2]);

        dp[i] = min(ret1,ret2);

    }*/



    cout << ans << endl;

    return 0;



}


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

#define sf1(n) scanf("%d",&n)

#define sf2(n,q) scanf("%d %d",&n,&q)

#define f first

#define ss second

#define pb push_back

#define lli long long

#define mem(ar,false) memset(ar,false,sizeof(ar))

#define mpr make_pair

#define endl "\n"

#define mod 1000000007

#define inf 9000000000000000000LL

using namespace std;



int ar[200005];

int n,k;

int dp[100005];

int func(int i)

{



    if(i==n-1)

        return 0;

    if(dp[i]!=-1)

        return dp[i];

    int ret = INT_MAX;



    for(int j=1;j<=k;j++)

    {

        if(i+j<n)

            ret = min(ret,abs(ar[i]-ar[i+j])+func(i+j));

    }

    return dp[i] = ret;

}



int main()

{

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

    //mem(dp,-1);

    cin >> n >> k;

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

        cin >> ar[i];



  //   int ans = func(0);

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

    {

        int ret = INT_MAX;

        for(int j=1;j<=k;j++)

        {

            if(i-j>=0)

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

        }

        dp[i] = ret;

    }



    cout << dp[n-1] << 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;





}