#include <iostream>
#include <cmath>
using namespace std;

int SW( int n, int k ){
    int h[n+1], custo[n+1];
    int menor = 100000000;
    
    for(int i=1;i<=n;i++) {
        cin >> h[i]; 
    }
    
    custo[1] = 0;
    custo[2] = abs(h[2]-h[1]);

    for( int i=3;i<=n;i++ ) {
        menor = 100000000;
        for(int j=1;j<=k;j++){
            if( i-j>0 ){
            custo[i]=custo[i-j] + abs(h[i]-h[i-j]);
            }
            if( custo[i]<menor ){
                menor = custo[i];
            }
        }
        custo[i] = menor;
    }

    return custo[n];
}


int main() {
int n,k;

cin >> n >> k;

cout << SW(n,k) << endl;

}