// Original C++ solution (slower):
/**

*   Bismillahir Rahmanir Rahim.

*   Imtiaz_rafi

*   PCIU, CHITTAGONG

**/

#include<bits/stdc++.h>

using namespace std;

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

#define filein freopen ("input.txt", "r", stdin)

#define fileout freopen ("output.txt", "w", stdout)

#define rep(i,a) for(ll i=0;i<a;i++)

#define rep1(i,begin,end) for(ll i=begin;i<=end;i++)

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

#define mem(dp,i) memset(dp,i,sizeof(dp))

#define pb push_back

#define pf push_front

#define mp make_pair

#define pi acos(-1)

// bitshift (1LL<<(3)) *2^3

typedef long long int ll;

typedef vector<ll>vi;

typedef map<ll,ll>m;

typedef map<char,ll>mc;

typedef set<ll>st;

typedef set<char>sc;

ll dx[] ={-1,+1,0,0,+1,-1,+1,-1};

ll dy[] ={0,0,-1,+1,+1,+1,-1,-1};

const ll mx = 1e5+123;

bool vis[mx];

vi adj[mx];

ll a,k,b[mx],dp[mx];

ll solve(ll i)

{

    if(i==a) return 0;

    if(dp[i]!=-1) return dp[i];

    ll x=INT_MAX;

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

    {

        if(i+j<=a)

            x=min(x,abs(b[i]-b[i+j])+solve(i+j));

    }

    return dp[i]=x;

}

int main()

{

    speed;

    cin>>a>>k;

    rep1(i,1,a)

        cin>>b[i];

    mem(dp,-1);

    cout<<solve(1)<<endl;

}




// Optimized solution to the same problem (faster):
/**

*   Bismillahir Rahmanir Rahim.

*   Imtiaz_rafi

*   PCIU, CHITTAGONG

**/

#include<bits/stdc++.h>

using namespace std;

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

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

#define rep(i,a) for(ll i=0;i<a;i++)

#define rep1(i,begin,end) for(ll i=begin;i<=end;i++)

#define mem(dp,i) memset(dp,i,sizeof(dp))

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

#define rall(v)   v.rbegin(),v.rend()

#define pb push_back

#define pf push_front

#define mp make_pair

#define pi acos(-1)

// bitshift (1LL<<(3)) *2^3

typedef long long int ll;

typedef vector<ll>vi;

typedef map<ll,ll>mi;

typedef pair<ll,ll>pll;

typedef vector<pll> vll;

typedef set<ll>st;

typedef set<char>sc;

ll dx[] = {-1,+1,0,0,+1,-1,+1,-1};

ll dy[] = {0,0,-1,+1,+1,+1,-1,-1};

const ll mx = 1e5+123;

const ll MOD = 1e9+7;



template <class T> T power(T a,T b){a%=MOD;if(!a) return 0;T p=1;while(b>0){if(b&1){p*=a;p%=MOD;}a*=a;a%=MOD;b=b>>1;}return p;}

template <class S> void print(vector<S> &v){for(ll i=0; i<(ll)v.size(); i++){cerr << v[i] << ' ';}cerr << endl;}

template <class T> T gcd(T a, T b){return (b!=0?gcd<T>(b,a%b):a);}

template <class T> T lcm(T a, T b){return (a/gcd<T>(a,b)*b);}



ll BS(ll l,ll r,ll a[],ll k){ll mid,i=-1;while(l<=r){mid=(l+r)>>1;if(a[mid]==k){return i=mid;/*r=mid-1;*/}if(a[mid]<k) l=mid+1;if(a[mid]>k) r=mid-1;}return i;}



ll dp[mx],b[mx],a,k;

ll solve(ll i)

{

    if(i==a)

        return 0;

    if(dp[i]!=-1)

        return dp[i];

    ll x=INT_MAX;

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

    {

        if(i+j<=a)

            x = min(x,abs(b[i]-b[i+j])+solve(i+j));

    }

    return dp[i] = x;

}



int main()

{

    speed();

    ll i;

    cin>>a>>k;

    rep1 (i,1,a)

        cin>>b[i];

    mem(dp,-1);

    cout<<solve(1)<<endl;



}




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





}