// Original C++ solution (slower):
#include <bits/stdc++.h>

#include <sstream>

using namespace std;

#define pr(x) cout<<x

#define prs cout<<" "

#define prn cout<<"\n"

#define sc(x) cin>>x

#define ll long long int



int n,k;

int a[1000005];

int dp[1000005];



int solve(int i)

{ 

    //cout<<n<<k<<endl;

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

    if(i >= n) return INT_MAX;

    if(dp[i] != -1)  return dp[i];

    vector<int> v(k,INT_MAX);

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

    {

        if(j+i > n-1)

         break;

        else

         v[j-1] = abs(a[i] - a[i+j])+solve(i+j);

    }

    dp[i] = *min_element(v.begin(), v.end());

    v.clear();

    return dp[i];

}



int main()

{

    ios :: sync_with_stdio(false); cin.tie(0);

    int t=1;

    //cin >> t;

    for(int i9 = 1; i9 <= t; i9++)

    {

        cin>>n>>k;

        memset(dp, -1, sizeof(dp));

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

          cin>>a[i];

        cout<<solve(0)<<endl;

        //cout<<"Cae #"<<i9<<": "<<res<<endl;

    }

}

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

#define F first

#define S second

#define REP(i, a, b, c) for(int i = a; i <= b; i+=c)

#define pb push_back

#define int long long int

#define MOD 1000000007

using namespace std;



/*class Graph {



  map<int, list<pair<int, int> > > l;



public:

  void addEdge(int x, int y, int cost) {

    l[x].pb(make_pair(y, cost));

    l[y].pb(make_pair(x, cost));

  }



  int dfs_helper(int n, int node, map<int, int> &vis, map<int, int> &count, int &ans) {

    //cout << node << " " ;

    vis[node] = 1;

    count[node] = 1;

    for (auto m : l[node]) {

      if (!vis[m.F]) {

        vis[m.F] = 1;

        count[node] += dfs_helper(m.F, vis, count, ans);

        ans += 2 * min(count[node], n - count[node]) * m.S.S;

      }

    }

    return count[node];

  }



  void dfs() {

    map<int, int> vis;

    map<int, int> count;

    int ans = 0;

    dfs_helper(0, vis, count, ans);

    cout << ans << endl;

  }



};*/



int dp[100005];



int f(int a[], int i, int n, int k)

{

  //cout << i << endl;

  if (i == (n - 1))

    return 0;

  if (i >= n)

    return INT_MAX;

  if (dp[i] != -1)

    return dp[i];

  int ans = INT_MAX;

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

  {

    if (i + j <= n - 1)

    {

      ans = min(ans, abs(a[i] - a[i + j]) + f(a, i + j, n, k));

    }

  }

  return dp[i] = ans;

}





int32_t main()

{

  ios :: sync_with_stdio(false); cin.tie(0);

  int t = 1;

  //cin >> t;

  for (int i9 = 1; i9 <= t; i9++)

  {

    /* Graph G;

     int n, x, y, cost;

     cin >> n;

     while (n--) {

       cin >> x >> y >> cost;

       G.addEdge(x, y, cost);

     }

     G.dfs();*/

    //freopen("ouputnew.txt", "w", stdout);

    //flclose(stdout);

    memset(dp, -1 , sizeof (dp));

    int n, k;

    cin >> n >> k;

    int a[n];

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

      cin >> a[i];

    cout << f(a, 0, n, k);

  }

}




// 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;





}