// Original C++ solution (slower):
#include <iostream>

#include<stdio.h>

#include<algorithm>

#include<bits/stdc++.h>

#include<cmath>

#include<cstring>

#include<cstdio>

#include<string.h>

#include<cmath>

#define maxn 1000010

#define ll long long

#define inf 0x3f3f3f3f

using namespace std;



ll h[maxn];

ll dp[maxn];

ll n,k;



int main()

{

    memset(h,0,sizeof h);

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



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

    {

        dp[i]=inf;

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

    }

    dp[1]=0;

    dp[0]=0;

    dp[2]=abs(h[2]-h[1]);

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

    {

        for(int j=i-1;j>=max(i-k,1ll);j--)

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

    }

    printf("%lld\n",dp[n]);

    return 0;

}


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

#include <string>

#include <cstdlib>

using namespace std;



#define READ                freopen("in.txt", "r", stdin)

#define WRITE               freopen("out.txt", "w", stdout)



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

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

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



#define CLR(a)              memset(a, 0, sizeof(a))

#define SET(a)              memset(a, -1, sizeof(a))



#define PI                  acos(-1.0)



#define pb                  push_back

#define mp                  make_pair

#define SORT(v)             sort(v.begin(), v.end())

#define REV(v)              reverse(v.begin(), v.end())

#define FOR(i, n)           for(int i=0; i<n; i++)

#define FOR1(i, n)          for(int i=1; i<=n; i++)

#define ROF(i, n)           for(int i=n-1; i>=0; i--)

#define ROF1(i, n)          for(int i=n; i>=1; i--)

#define LOOP(i, x, y)       for(int i=x; i<=y; i++)

#define fastIO()            ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);





typedef long long              LL;

typedef unsigned long long     ULL;

typedef vector<int>            VI;

typedef pair<int, int>         PII;

typedef pair<string, int>      PSI;

typedef pair<char, int>        PCI;



const int MAX = 100005;

const LL LMAX = 12158598925;

const double eps = 1e-9;

const int INF = 0x3f3f3f3f;



int dx[] = {-1, 0, 1, 0};

int dy[] = {0, 1, 0, -1};

int kx[] = {1, 1, 2, 2, -1, -1, -2, -2};

int ky[] = {2, -2, 1, -1, 2, -2, 1, -1};



LL dp[MAX], arr[MAX];





int main()

{

//    READ;

//    WRITE;



    int n, k; sii(n, k);

    FOR1(i, n) scanf("%lld", &arr[i]);



    dp[1] = 0;



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

        LL p = INT_MAX;

        for(int j=1; j<=min(k, i-1); j++)

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



        dp[i] = p;

    }



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





}