/*#include <bits/stdc++.h>
#define cross(a,b) (conj(a)*(b)).imag()
#define dot(a,b) (conj(a)*(b)).real()
#define ld long double
#define ll long long
#define vec(a,b) (b-a)
#define pi acos(-1)
//#define x real()
//#define y imag()


using namespace std;
typedef complex <ld> point;
typedef vector<point> polygon;
typedef tuple<int,int,int> line; // represents a b c of line

vector <ll> v;
//vector <ll> vc;

//ll mod = 1e9 +7;
//ll fact[1000005], inv[1000005];
//int p;


bool prime(ll n)
{
   if(n==1)
    return false;
   for(int i=2; i<=sqrt(n); ++i)
   {
       if(n%i==0)
       {
           return false;
       }
   }
   return true;
}

/*unsigned long long fp(ll x, ll y)
{
    if(y==0)
    {
        return 1;
    }
    ll z = fp(x,(y/2));

    if(y%2==0)
    {
        return (z*z)%p;
    }
    else
        return (((z*z)%p)*(x%p))%p;
}*/
/*unsigned long long fp(ll x, ll y)
{
    if(y==0)
    {
        return 1;
    }
    ll z = fp(x,(y/2));

    if(y%2==0)
    {
        return (z*z)%mod;
    }
    else
        return (((z*z)%mod)*(x%mod))%mod;
}
void pre()
{
  fact[0]=inv[0]=1;
  for(int i=1; i<=1000005; ++i)
  {
      fact[i]=(fact[i-1]*i)%mod;
      inv[i]=fp(fact[i], mod-2);
  }
}
unsigned ll ncr(int n, int r)
{
    return (((fact[n]*inv[n-r])%mod) * inv[r])%mod;
}*/

/*int main()
{
    //pre();
    int n, ans=0;
    ll c;
    cin >> n;
    for(int k=1; k<=n; ++k)
    {
        c=0;
        bool arr[30005]={false};
        if(prime(k))
            continue;
        for(ll i=1; i*i<=k; ++i)
        {
            if(k%i==0 && prime(i) && arr[i]==false)
            {
                c++;
                arr[i]=true;
            }
            if(k%i==0 && prime(k/i) && arr[k/i]==false)
            {
                    c++;
                    arr[k/i]=true;
            }
        }
        if(c==2)
            ans++;
    }
    cout << ans << endl;*/
/*cin >>p;
int cnt=0;
bool ch=1;
for(int i=1; i<p; ++i)
{
    ch=1;
    for(int j=1; j<=p-2; ++j)
    {
            if(((fp(i,j)-1)+p)%p==0)
            {
                ch=0;
            }
            if( ((fp(i,p-1)-1)+p)%p!=0)
            {
                ch=0;
            }
            if(!ch)
            {
                break;
            }
    }
    if(ch)
        cnt++;
}
cout << cnt << endl;*/
//return 0;
//}

#include <bits/stdc++.h>
#define ll long long

using namespace std;

vector<ll> A(6);
/*unsigned ll gcd(ll x, ll y)
{
    if(y==0)
    {
        return x;
    }
    return gcd(y,x%y);
}*/
/*void gen(ll ind, string t)
{
    if(ind==s.size())
    {
        if(t.size()==3)
            v.push_back(t);
        return;
    }
    gen(ind+1, t+s[ind]);
    gen(ind+1, t);
}*/

/*void f(int g)
{
    for(ll i=2; i*i<=g; ++i)
    {
        while(g%i==0)
        {
            v.push_back(i);
            g/=i;
        }
    }
    if(g>1)
    {
        v.push_back(g);
    }
}*/
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int n, x;
    cin >> n >> x;
    int h[n];
    for(int i=0; i<n; ++i)
    {
        cin >> h[i];
    }
    int ans[n];
    ans[0]=0;
    ans[1]=abs(h[0]-h[1]);
    for(int i=2; i<n; ++i)
    {
        ans[i]=1e9;
        for(int j=i-1; j>=max(0,i-x); --j)
        {
            ans[i]=min(ans[j]+abs(h[i]-h[j]),ans[i]);
        }
    }
    cout << ans[n-1] << endl;
    /*cin >> n >> d;
    for(int i=0; i<n; ++i)
    {
        cin >> arr[i];
        s+=arr[i]+'0';
    }
    gen(0,"");
    cout << check() << endl;*/
    /*for(int i=0; i<v.size(); ++i)
    {
        cout << v[i][0]-'0' + v[i][v[i].size()-1]-'0' << " ";
        cout << v[i] << "\n";
    }*/
    return 0;
}





Measured memory_bytes: 1024