/***************************************************
 * Problem Name : B - Frog 2.cpp
 * Problem Link : https://atcoder.jp/contests/dp/tasks/dp_b
 * OJ           : atCoder
 * Verdict      : Trying
 * Date         : 2019-01-26
 * Problem Type : Basic DP
 * Author Name  : Saikat Sharma
 * University   : CSE, MBSTU
 ***************************************************/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<climits>
#include<cstring>
#include<string>
#include<sstream>
#include<cmath>
#include<vector>
#include<queue>
#include<cstdlib>
#include<deque>
#include<stack>
#include<map>
#include<set>
#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define FileRead freopen ("/home/saikat/Desktop/logN/input.txt", "r", stdin);
#define FileWrite freopen ("/home/saikat/Desktop/logN/output.txt", "w", stdout);
#define SET(a,v) memset(a,v,sizeof(a))
#define pii pair<int,int>
#define pll pair <ll, ll>
#define debug cout <<"#########\n";
#define nl cout << "\n";
#define sp cout << " ";
#define sl(n) scanf("%lld", &n)
#define sf(n) scanf("%lf", &n)
#define si(n) scanf("%d", &n)
#define ss(n) scanf("%s", n)
#define pf(n) scanf("%d", n)
#define pfl(n) scanf("%lld", n)
#define all(v) v.begin(), v.end()
#define Pow2(x) ((x)*(x))
#define Mod(x, m) ((((x) % (m)) + (m)) % (m))
#define Max3(a, b, c) max(a, max(b, c))
#define Min3(a, b, c) min(a, min(b, c))
#define pb push_back
#define mk make_pair
#define MAX 100005
#define INF 1000000000
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

template <typename T>
std::string NumberToString ( T Number ) {
    std::ostringstream ss;
    ss << Number;
    return ss.str();
}
ll lcm (ll a, ll b) {
    return a * b / __gcd (a, b);
}
/************************************ Code Start Here ******************************************************/
int ar[MAX];
int dp[MAX][103];
int dpp[MAX];
int iterative (int n, int k) {
    //~ for (int i = 1; i <= n; i++) {
    //~ for (int j = 1; j <= k; j++) {
    //~ dp[i][j] = INF;
    //~ }
    //~ }
    //~ for (int i = 1; i <= n; i++) {
    //~ dpp[i] = INF;
    //~ }
    dpp[0] = INF;
    dpp[1] = 0;
    dpp[2] = abs (ar[2] - ar[1]);

    for (int i = 3; i <= n; i++) {
        int mn  = INF;

        for (int j = 1; j <= min (k, i); j++) {
            int tmp = abs (ar[i] - ar[i - j]);
            //~ cout << tmp << " tmp\n";
            tmp += dpp[i - j];
            //~ cout << i << " " << j << " " << tmp << "\n";
            mn = min (mn, tmp);
        }

        //~ cout << i << " "<< mn << " -\n";
        dpp[i] = mn;
    }

    return dpp[n];
    //~ for (int i = 2; i <= n; i++) {
    //~ int tmp[MAX];
    //~ int mn = INF;
    //~ for (int j = 1; j <= k; j++) {
    //~ if (j < i) {
    //~ tmp[j] = abs (ar[i] - ar[i - j]);
    //~ mn = min(mn, dp[i-j][j]);
    //~ }
    //~ }
    //~ for (int j = 1; j <= k; j++) {
    //~ if (j < i) {
    //~ dp[i][j] = dp[i][j]
    //~ }
    //~ }
    //~ }
    //~ int kochi = 0;
    //~ return kochi;
}
int main () {
    //~ __FastIO;
    int n, k;
    cin >> n >> k;

    for (int i = 1; i <= n; i++) {
        cin >> ar[i];
    }

    int ans = iterative (n, k);
    cout << ans << "\n";
    return 0;
}



Measured memory_bytes: 2688