// Original C++ solution (slower):


    #include <bits/stdc++.h>

    using namespace std;

    #define INF 2000000000

    typedef long long int ll;

    typedef pair<ll,ll> ii;

    typedef pair<ii,ll> iii;

    typedef vector<ll> vi;

    typedef vector<ii> vii;

    #define getbit(n,i) (((n)&(1<<(i)))!=0)

    #define setbit0(n,i) ((n)&(~(1<<(i))))

    #define setbit1(n,i) ((n)|(1<<(i)))

    #define togglebit(n,i) ((n)^(1<<(i)))

    #define lastone(n) ((n)&(-(n)))

    #define read freopen("debug\\in.txt","r",stdin)

    #define write freopen("debug\\out.txt","w",stdout)

    #define fi first

    #define se second

    #define getI(a) scanf("%d",&a)

    #define getII(a,b) scanf("%d%d",&a,&b)

    #define PI (acos(-1))

    #define fastread ios_base::sync_with_stdio(false);cin.tie(NULL)

    #define mod (1000000007)

    #define asz 500005

    #define pb push_back

    #define ppf pop_front

    #define ppb pop_back

    #define pf push_front

    ll k,n;

    ll dp[200005];

    ll h[200005];

   // ll mini=1e9;

    ll recurse(ll pos){

      if(pos==n)

      return 0;

      ll i;

      if(dp[pos]!=-1)

      return dp[pos];

      ll mini=1e9;

      for(i=1;i<=k&&pos+i<=n;i++){

        mini=min(mini,recurse(pos+i)+abs(h[pos]-h[pos+i]));

      }

      return dp[pos]=mini;

    }

    int main()

    {

        fastread;

        ll i,j,l,m,t,q,r,c;

        for(i=1;i<=200005;i++)

        dp[i]=-1;

        cin>>n>>k;

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

        cin>>h[i];

        cout<<recurse(1);

        return 0;

    }

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

using namespace std;

#define INF 2000000000

typedef long long int ll;

typedef pair<ll,ll> ii;

typedef pair<ii,ll> iii;

typedef vector<ll> vi;

typedef vector<ii> vii;

#define getbit(n,i) (((n)&(1<<(i)))!=0)

#define setbit0(n,i) ((n)&(~(1<<(i))))

#define setbit1(n,i) ((n)|(1<<(i)))

#define togglebit(n,i) ((n)^(1<<(i)))

#define lastone(n) ((n)&(-(n)))

#define read freopen("debug\\in.txt","r",stdin)

#define write freopen("debug\\out.txt","w",stdout)

#define fi first

#define se second

#define getI(a) scanf("%d",&a)

#define getII(a,b) scanf("%d%d",&a,&b)

#define PI (acos(-1))

#define fastread ios_base::sync_with_stdio(false);cin.tie(NULL)

#define mod (1000000007)

#define asz 500005

#define pb push_back

#define ppf pop_front

#define ppb pop_back

#define pf push_front

ll a[500005];

ll dp[500006];

int main()

{

    ll n,i,j,k,l,m;

    scanf("%lld%lld",&n,&k);

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

    	scanf("%lld",&a[i]);

    	dp[i]=1e9;

	}

	dp[1]=0;

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

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

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

	}

	cout<<dp[n];

    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;





}