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

using namespace std;

typedef		long long       ll;

typedef		vector<ll>      vll;

#define     fi              first

#define     se              second

#define		pb              push_back

#define		nn              "\n"

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

#define		zz(v)           (ll)v.size()

#define 	S(a)            scanf("%lld",&a)

#define 	SS(a,b)         scanf("%lld %lld",&a,&b)

#define 	SSS(a,b,c)      scanf("%lld %lld %lld",&a,&b,&c)

#define		ss              ' '

#define     pii             pair<ll,ll>

const double eps = 1e-8;



ll ar[100010];

ll dp[100010];

ll n,k;



int main()

{

    memset(dp,-1,sizeof(dp));

    cin>>n>>k;

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

        cin>>ar[i];

    dp[1]=0;

    dp[0]=1e6;

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

    {

        ll ans=INT_MAX;

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

        {

            if(i-j>=0)

            {

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

            }

            else

                break;

        }

        dp[i]=ans;

    }

    cout<<dp[n]<<ss;

}














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

using namespace std;

typedef		 long long int    ll;

typedef		vector<ll>      vll;

#define     fi              first

#define     print(v)        for(ll i:v) cout<<i<<ss

#define     se              second

#define		pb              push_back

#define		nn              "\n"

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

#define		zz(v)           (ll)v.size()

#define 	S(a)            scanf("%lld",&a)

#define 	SS(a,b)         scanf("%lld %lld",&a,&b)

#define 	SSS(a,b,c)      scanf("%lld %lld %lld",&a,&b,&c)

#define		ss              ' '

#define     pii             pair<ll,ll>

#define		gcd(a,b)        __gcd(a,b)

#define		lcm(a,b)        (a*b)/gcd(a,b)

const double eps = 1e-8;







int main()

{



    ll n,k;

    cin>>n>>k;

    ll ar[n+10];

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

    {

        cin>>ar[i];

    }



    ll dp[n+10];



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

    {

        dp[i]=1e18;

    }

    dp[1]=0;

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

    {

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

        {

            if(i+j>n)

                continue;



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

        }

   



    }



    



     cout<<dp[n]<<nn;









}





/* Final check before submit :



1. array size or integer overflow,like uninitialised 0 index.



2. Think twice,code once.check all possible counter test case.



3. Be careful about corner case! n=1?k=1? something about 0?



4. avoid stupid mistake!complexity(t/m)?precision error?submit same code twice?



5. if got WA than remember that you are missing something common.

   *** Be confident!!! who knows? may be your one step back to AC ***

4. minus mod ans=(ans-k+mod)%mod;



*/










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





}