// Original C++ solution (slower):
#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;

}


// Optimized solution to the same problem (faster):
#include <cstdio>

#include <algorithm>

#include <cmath>

using namespace std;



int h[(int)1e5], dp[(int)1e5], K, N;





int main()

{

	K = N = 0;

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

	{

		dp[j] = 1e9;

	}

	scanf("%d %d", &N, &K);

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

	{

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

	}

	dp[1] = 0;

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

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

	{

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

		{

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

		}

	}

	printf("%d\n", 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;





}