(N,K) = list(map(int,input().split()))                                                                                                                       
h = list(map(int,input().split()))                                                                                                                           
dp = [float('inf')] * N                                                                                                                                      
dp[0] = 0                                                                                                                                                    
                                                                                                                                                             
def chmin(lst,a,b):                                                                                                                                          
    if lst[a]>b:                                                                                                                                             
        lst[a]=b                                                                                                                                             
                                                                                                                                                             
def chmax(lst,a,b):                                                                                                                                          
    if lst[a]<b:                                                                                                                                             
        lst[a]=b                                                                                                                                             
                                                                                                                                                             
for i in range(1,N):                                                                                                                                         
    for k in range(1,K+1):                                                                                                                                   
        if i >= k:                                                                                                                                           
            chmin(dp,i,dp[i-k]+abs(h[i-k]-h[i]))                                                                                                             
                                                                                                                                                             
print(dp[N-1])   

### FIXED CODE ###

N = int(input())                                                                                                                                             
h = list(map(int,input().split()))                                                                                                                           
dp = [float('inf')] * N                                                                                                                                      
dp[0] = 0                                                                                                                                                    
                                                                                                                                                             
def chmin(lst,a,b):                                                                                                                                          
    if lst[a]>b:                                                                                                                                             
        lst[a]=b                                                                                                                                             
                                                                                                                                                             
def chmax(lst,a,b):                                                                                                                                          
    if lst[a]<b:                                                                                                                                             
        lst[a]=b                                                                                                                                             
                                                                                                                                                             
for i in range(1,N):                                                                                                                                         
    chmin(dp,i,dp[i-1]+abs(h[i-1]-h[i]))                                                                                                                     
    if i>1:                                                                                                                                                  
        chmin(dp,i,dp[i-2]+abs(h[i-2]-h[i]))                                                                                                                 
                                                                                                                                                             
print(dp[N-1])