#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif

#define ll          long long
#define pb          push_back
#define eb          emplace_back
//#define mp          make_pair
#define pi          pair<int,int>
#define pii         pair<pi,int>
#define vi          vector<int>
#define vii         vector<vector<int> >
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (int)x.size()
#define endl        '\n'
#define rep(i,a,b)  for(int i=a;i<b;i++)
#define repr(i,a,b) for(int i=a;i>b;i--)
using namespace std;

template<typename T> void scan(T &x) 
{ 
    x = 0; 
    bool neg = 0; 
    register T c = getchar(); 
  
    if (c == '-') 
        neg = 1, c = getchar(); 
  
    while ((c < 48) || (c > 57)) 
        c = getchar(); 
  
    for ( ; c < 48||c > 57 ; c = getchar()); 
  
    for ( ; c > 47 && c < 58; c = getchar() ) 
        x= (x << 3) + ( x << 1 ) + ( c & 15 ); 
  
    if (neg) x *= -1; 
} 
  
template<typename T> void print(T n) 
{ 
    bool neg = 0; 
  
    if (n < 0) 
        n *= -1, neg = 1; 
  
    char snum[65]; 
    int i = 0; 
    do
    { 
        snum[i++] = n % 10 + '0'; 
        n /= 10; 
    } 
  
    while (n); 
    --i; 
  
    if (neg) 
        putchar('-'); 
  
    while (i >= 0) 
        putchar(snum[i--]); 
  
    //putchar('\n'); 
} 

string to_string(string s) {
	return '"' + s + '"';
}

string to_string(const char* s) {
	return to_string((string) s);
}

string to_string(bool b) {
	return (b ? "true" : "false");
}

string to_string(char ch) {
	return string("'")+ch+string("'");
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
	return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <class InputIterator>
string to_string (InputIterator first, InputIterator last) {
  bool start = true;
  string res = "{";
  while (first!=last) {
  	if (!start) {
  		res += ", ";
  	}
  	start = false;
  	res += to_string(*first);
    ++first;
  }
  res += "}";
  return res;
}

template <typename A>
string to_string(A v) {
	bool first = true;
	string res = "{";
	for (const auto &x : v) {
		if (!first) {
			res += ", ";
		}
		first = false;
		res += to_string(x);
	}
	res += "}";
	return res;
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
	cerr << " " << to_string(H);
	debug_out(T...);
}

template <typename A, typename B>
istream& operator>>(istream& input,pair<A,B>& x){
	input>>x.F>>x.S;
	return input;
}

template <typename A>
istream& operator>>(istream& input,vector<A>& x){
	for(auto& i:x)
		input>>i;
	return input;
}
ll powz(int x,int y,int mod)
{  ll res=1,a,b;
 a=x;b=y;
 while(b)
 {
     if(b&1){
         res*=a;
         
     }
     b/=2;
     a*=a;
a%=mod;
res%=mod;
     
 }
    return res;
    
    
}
const ll mod=1e9+7;


#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

ll nCr(ll n, ll k) 
{ 
    ll res = 1; 
//if(k==0)return 1;
  if(n<k)return 0;
    // Since C(n, k) = C(n, n-k) 
    if ( k > n - k ) 
        k = n - k; 
  
    // Calculate value of [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1] 
    for (ll i = 0; i < k; ++i) 
    { 
        res *= (n - i); 
        res /= (i + 1); 
        res%=mod;
    } 
  
    return res; 
}


mt19937 rng32((int)chrono::steady_clock::now().time_since_epoch().count());

void solve()
{
/*clock_t begin=clock();
cout<<"Time in seconds"<<(double) (clock() - begin) / CLOCKS_PER_SEC;*/
int n,k;
cin>>n>>k;
vi a(n);
cin>>a;
vi dp(n+1,1e9);
dp[0]=0;
for(int i=1;i<n;i++)
{
for(int j=1;j<=k;j++)
{
if(i>=j){
dp[i]=min(dp[i],dp[i-j]+abs(a[i]-a[i-j]));
//dp[i]=min(dp[i],dp[i-j]+abs(a[i]-a[i-j]));
}
}
}
cout<<dp[n-1];
}




int main()
{

#ifdef LOCAL
freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
#endif


	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t=1;
	//cin>>t;
	while(t--){
		solve();
	}
	return 0;
}

/* 
./a.out < input_file > output_file  
g++ practice.cpp -DLOCAL
*/

Measured memory_bytes: 1024