// Slower C++ solution:
#include <bits/stdc++.h>

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

#define ii          pair<int,int>

#define F           first

#define S           second

#define pb          push_back

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

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

#define fore(i,a,b) for(int i = a;i < b; i+= 1)

#define forr(i,a)   for(int i = a; i >= 0; i--)

#define fori(i,m)   for(auto i = m.begin(); i != m.end(); i++) 

#define w(t)        while(t--)

#define sz(s)       int(s.size())

#define cls(a,car)  memset(a,car,sizeof (a))

#define db(x)       cerr << #x << " is " << x << '\n'

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

#define EjecuteTime cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"

#define angle(x)    double(x * acos(-1) / 180.0)

using namespace std;

typedef long long   ll;

typedef vector<int> vi;

typedef vector<ii>  vii;

const int N = 1 * 1e5 + 5;

const ll mod = 1e9 + 7;

const double E = 1e-9;

//int dx[] = {1, 0}, dy[] = {0, 1};

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

//int dx[] = {2, 1, -1, -2, -2, -1, 1, 2}, dy[] = {1, 2, 2, 1, -1, -2, -2, -1};

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

// fflush(stdout)

// cout << flush

// a + b = a ^ b + 2 * (a & b)

int v[N];

int dp[N];

int n, k;

int f(int i){

    if(i == n - 1)return 0;

    int &ans = dp[i];

    if(ans == -1){

        ans = 1e9;

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

            if(i + j < n)

                ans = min(ans, f(i + j) + abs(v[i] - v[i + j]));

            else

                break;

    }

    return ans;

}

int main(){

    //FL;

    FIN;

    cin >> n >> k;

    fore(i, 0, n)cin >> v[i];

    cls(dp, -1);

    cout << f(0) << '\n';

    EjecuteTime;

    return 0;

}       

// Faster C++ solution for the same problem (measured speedup: 1.4191668589015571x):
#include <bits/stdc++.h>

using namespace std;

#define fix(f,n) std::fixed<<std::setprecision(n)<<f

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

typedef long long int ll;

typedef unsigned long long int ull;

#define vi vector<int>

#define pii pair<int,int>

#define vii vector<pii>

#define max(a,b) ((a>b)?a:b)

#define min(a,b) ((a>b)?b:a)

#define max3(a,b,c) ((a>b)?(a>c)?a:c:(b>c)?b:c)

#define min3(a,b,c) ((a<b)?(a<c)?a:c:(b<c)?b:c)

#define REP(i,a,n) for(ll i=a;i<n;i++)

#define pb push_back

#define mp make_pair



int main(){

	fast;

	int test=1;

	//cin >> test;

	while(test--){

	    int n,k;

	    cin >> n >> k;

	    ll dp[n+1]={};

	    ll arr[n+1];

	    dp[1]=0;

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

	        cin >> arr[i];

	    }

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

	        ll temp=1000000000000000000;

	        for(int j=(i-1) ; j>=1 ; j--){

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

	        }

	        dp[i]=temp;

	    }

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

	        ll temp=1000000000000000000;

	        for(int j=(i-1) ; j>=(i-k) ; j--){

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

	        }

	        dp[i]=temp;

	    }

	    cout << dp[n] << endl;

	}

	return 0;

}