import java.util.Scanner;
import java.util.*;
public class Main {
	
	
	
	public static int Frog2(int n,int k, int h[]) {
		int dp[] = new int[n];
		dp[0] = 0;
		dp[1] = Math.abs(h[1]-h[0]);
		
		for(int i=2;i<n;i++) {
			int min = Integer.MAX_VALUE;
			if(i-k>=0) {
				for(int j=i-1;j>=i-k;j--) {
					int minCount = Math.abs(h[i]-h[j])+dp[j];
					if(minCount < min) {
						min = minCount;
					}	
				}
			}else {
				for(int j=i-1;j>=0;j--) {
					int minCount = Math.abs(h[i]-h[j])+dp[j];
					if(minCount < min) {
						min = minCount;
					}	
				}
			}
			
			dp[i] = min;
		}
		
		return dp[n-1];
	}
    
    public static void main(String[] arg) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int k = s.nextInt();
        int h[] = new int[n];
        for(int i=0;i<n;i++) {
        	h[i] = s.nextInt();
        }
        System.out.println(Frog2(n,k,h));
        
    }
} import java.util.Scanner; Scanner java.util import java.util.*; util.* java public class Main {
	
	
	
	public static int Frog2(int n,int k, int h[]) {
		int dp[] = new int[n];
		dp[0] = 0;
		dp[1] = Math.abs(h[1]-h[0]);
		
		for(int i=2;i<n;i++) {
			int min = Integer.MAX_VALUE;
			if(i-k>=0) {
				for(int j=i-1;j>=i-k;j--) {
					int minCount = Math.abs(h[i]-h[j])+dp[j];
					if(minCount < min) {
						min = minCount;
					}	
				}
			}else {
				for(int j=i-1;j>=0;j--) {
					int minCount = Math.abs(h[i]-h[j])+dp[j];
					if(minCount < min) {
						min = minCount;
					}	
				}
			}
			
			dp[i] = min;
		}
		
		return dp[n-1];
	}
    
    public static void main(String[] arg) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int k = s.nextInt();
        int h[] = new int[n];
        for(int i=0;i<n;i++) {
        	h[i] = s.nextInt();
        }
        System.out.println(Frog2(n,k,h));
        
    }
} Main public static int Frog2(int n,int k, int h[]) {
		int dp[] = new int[n];
		dp[0] = 0;
		dp[1] = Math.abs(h[1]-h[0]);
		
		for(int i=2;i<n;i++) {
			int min = Integer.MAX_VALUE;
			if(i-k>=0) {
				for(int j=i-1;j>=i-k;j--) {
					int minCount = Math.abs(h[i]-h[j])+dp[j];
					if(minCount < min) {
						min = minCount;
					}	
				}
			}else {
				for(int j=i-1;j>=0;j--) {
					int minCount = Math.abs(h[i]-h[j])+dp[j];
					if(minCount < min) {
						min = minCount;
					}	
				}
			}
			
			dp[i] = min;
		}
		
		return dp[n-1];
	} Frog2 {
		int dp[] = new int[n];
		dp[0] = 0;
		dp[1] = Math.abs(h[1]-h[0]);
		
		for(int i=2;i<n;i++) {
			int min = Integer.MAX_VALUE;
			if(i-k>=0) {
				for(int j=i-1;j>=i-k;j--) {
					int minCount = Math.abs(h[i]-h[j])+dp[j];
					if(minCount < min) {
						min = minCount;
					}	
				}
			}else {
				for(int j=i-1;j>=0;j--) {
					int minCount = Math.abs(h[i]-h[j])+dp[j];
					if(minCount < min) {
						min = minCount;
					}	
				}
			}
			
			dp[i] = min;
		}
		
		return dp[n-1];
	} int dp[] = new int[n]; dp new int[n] n dp[0] = 0 dp[0] dp 0 0 dp[1] = Math.abs(h[1]-h[0]) dp[1] dp 1 Math.abs(h[1]-h[0]) Math.abs Math h[1]-h[0] h[1] h 1 h[0] h 0 for(int i=2;i<n;i++) {
			int min = Integer.MAX_VALUE;
			if(i-k>=0) {
				for(int j=i-1;j>=i-k;j--) {
					int minCount = Math.abs(h[i]-h[j])+dp[j];
					if(minCount < min) {
						min = minCount;
					}	
				}
			}else {
				for(int j=i-1;j>=0;j--) {
					int minCount = Math.abs(h[i]-h[j])+dp[j];
					if(minCount < min) {
						min = minCount;
					}	
				}
			}
			
			dp[i] = min;
		} int i=2; int i=2; i 2 i<n i n i++ i++ i {
			int min = Integer.MAX_VALUE;
			if(i-k>=0) {
				for(int j=i-1;j>=i-k;j--) {
					int minCount = Math.abs(h[i]-h[j])+dp[j];
					if(minCount < min) {
						min = minCount;
					}	
				}
			}else {
				for(int j=i-1;j>=0;j--) {
					int minCount = Math.abs(h[i]-h[j])+dp[j];
					if(minCount < min) {
						min = minCount;
					}	
				}
			}
			
			dp[i] = min;
		} {
			int min = Integer.MAX_VALUE;
			if(i-k>=0) {
				for(int j=i-1;j>=i-k;j--) {
					int minCount = Math.abs(h[i]-h[j])+dp[j];
					if(minCount < min) {
						min = minCount;
					}	
				}
			}else {
				for(int j=i-1;j>=0;j--) {
					int minCount = Math.abs(h[i]-h[j])+dp[j];
					if(minCount < min) {
						min = minCount;
					}	
				}
			}
			
			dp[i] = min;
		} int min = Integer.MAX_VALUE; min Integer.MAX_VALUE Integer Integer.MAX_VALUE if(i-k>=0) {
				for(int j=i-1;j>=i-k;j--) {
					int minCount = Math.abs(h[i]-h[j])+dp[j];
					if(minCount < min) {
						min = minCount;
					}	
				}
			}else {
				for(int j=i-1;j>=0;j--) {
					int minCount = Math.abs(h[i]-h[j])+dp[j];
					if(minCount < min) {
						min = minCount;
					}	
				}
			} i-k>=0 i-k i k 0 {
				for(int j=i-1;j>=i-k;j--) {
					int minCount = Math.abs(h[i]-h[j])+dp[j];
					if(minCount < min) {
						min = minCount;
					}	
				}
			} for(int j=i-1;j>=i-k;j--) {
					int minCount = Math.abs(h[i]-h[j])+dp[j];
					if(minCount < min) {
						min = minCount;
					}	
				} int j=i-1; int j=i-1; j i-1 i 1 j>=i-k j i-k i k j-- j-- j {
					int minCount = Math.abs(h[i]-h[j])+dp[j];
					if(minCount < min) {
						min = minCount;
					}	
				} {
					int minCount = Math.abs(h[i]-h[j])+dp[j];
					if(minCount < min) {
						min = minCount;
					}	
				} int minCount = Math.abs(h[i]-h[j])+dp[j]; minCount Math.abs(h[i]-h[j])+dp[j] Math.abs(h[i]-h[j]) Math.abs Math h[i]-h[j] h[i] h i h[j] h j dp[j] dp j if(minCount < min) {
						min = minCount;
					} minCount < min minCount min {
						min = minCount;
					} min = minCount min minCount {
				for(int j=i-1;j>=0;j--) {
					int minCount = Math.abs(h[i]-h[j])+dp[j];
					if(minCount < min) {
						min = minCount;
					}	
				}
			} for(int j=i-1;j>=0;j--) {
					int minCount = Math.abs(h[i]-h[j])+dp[j];
					if(minCount < min) {
						min = minCount;
					}	
				} int j=i-1; int j=i-1; j i-1 i 1 j>=0 j 0 j-- j-- j {
					int minCount = Math.abs(h[i]-h[j])+dp[j];
					if(minCount < min) {
						min = minCount;
					}	
				} {
					int minCount = Math.abs(h[i]-h[j])+dp[j];
					if(minCount < min) {
						min = minCount;
					}	
				} int minCount = Math.abs(h[i]-h[j])+dp[j]; minCount Math.abs(h[i]-h[j])+dp[j] Math.abs(h[i]-h[j]) Math.abs Math h[i]-h[j] h[i] h i h[j] h j dp[j] dp j if(minCount < min) {
						min = minCount;
					} minCount < min minCount min {
						min = minCount;
					} min = minCount min minCount dp[i] = min dp[i] dp i min return dp[n-1]; dp[n-1] dp n-1 n 1 int n n int k k int h[] h public static void main(String[] arg) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int k = s.nextInt();
        int h[] = new int[n];
        for(int i=0;i<n;i++) {
        	h[i] = s.nextInt();
        }
        System.out.println(Frog2(n,k,h));
        
    } main {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int k = s.nextInt();
        int h[] = new int[n];
        for(int i=0;i<n;i++) {
        	h[i] = s.nextInt();
        }
        System.out.println(Frog2(n,k,h));
        
    } Scanner s = new Scanner(System.in); s new Scanner(System.in) int n = s.nextInt(); n s.nextInt() s.nextInt s int k = s.nextInt(); k s.nextInt() s.nextInt s int h[] = new int[n]; h new int[n] n for(int i=0;i<n;i++) {
        	h[i] = s.nextInt();
        } int i=0; int i=0; i 0 i<n i n i++ i++ i {
        	h[i] = s.nextInt();
        } {
        	h[i] = s.nextInt();
        } h[i] = s.nextInt() h[i] h i s.nextInt() s.nextInt s System.out.println(Frog2(n,k,h)) System.out.println System.out System System.out Frog2(n,k,h) Frog2 n k h String[] arg arg public class Main {
	
	
	
	public static int Frog2(int n,int k, int h[]) {
		int dp[] = new int[n];
		dp[0] = 0;
		dp[1] = Math.abs(h[1]-h[0]);
		
		for(int i=2;i<n;i++) {
			int min = Integer.MAX_VALUE;
			if(i-k>=0) {
				for(int j=i-1;j>=i-k;j--) {
					int minCount = Math.abs(h[i]-h[j])+dp[j];
					if(minCount < min) {
						min = minCount;
					}	
				}
			}else {
				for(int j=i-1;j>=0;j--) {
					int minCount = Math.abs(h[i]-h[j])+dp[j];
					if(minCount < min) {
						min = minCount;
					}	
				}
			}
			
			dp[i] = min;
		}
		
		return dp[n-1];
	}
    
    public static void main(String[] arg) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int k = s.nextInt();
        int h[] = new int[n];
        for(int i=0;i<n;i++) {
        	h[i] = s.nextInt();
        }
        System.out.println(Frog2(n,k,h));
        
    }
} public class Main {
	
	
	
	public static int Frog2(int n,int k, int h[]) {
		int dp[] = new int[n];
		dp[0] = 0;
		dp[1] = Math.abs(h[1]-h[0]);
		
		for(int i=2;i<n;i++) {
			int min = Integer.MAX_VALUE;
			if(i-k>=0) {
				for(int j=i-1;j>=i-k;j--) {
					int minCount = Math.abs(h[i]-h[j])+dp[j];
					if(minCount < min) {
						min = minCount;
					}	
				}
			}else {
				for(int j=i-1;j>=0;j--) {
					int minCount = Math.abs(h[i]-h[j])+dp[j];
					if(minCount < min) {
						min = minCount;
					}	
				}
			}
			
			dp[i] = min;
		}
		
		return dp[n-1];
	}
    
    public static void main(String[] arg) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int k = s.nextInt();
        int h[] = new int[n];
        for(int i=0;i<n;i++) {
        	h[i] = s.nextInt();
        }
        System.out.println(Frog2(n,k,h));
        
    }
} Main
import java.util.Scanner;
import java.util.*;
public class Main {
	
	
	
	public static int Frog2(int n,int k, int h[]) {
		int dp[] = new int[n];
		dp[0] = 0;
		dp[1] = Math.abs(h[1]-h[0]);
		
		for(int i=2;i<n;i++) {
			int min = Integer.MAX_VALUE;
			if(i-k>=0) {
				for(int j=i-1;j>=i-k;j--) {
					int minCount = Math.abs(h[i]-h[j])+dp[j];
					if(minCount < min) {
						min = minCount;
					}	
				}
			}else {
				for(int j=i-1;j>=0;j--) {
					int minCount = Math.abs(h[i]-h[j])+dp[j];
					if(minCount < min) {
						min = minCount;
					}	
				}
			}
			
			dp[i] = min;
		}
		
		return dp[n-1];
	}
    
    public static void main(String[] arg) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int k = s.nextInt();
        int h[] = new int[n];
        for(int i=0;i<n;i++) {
        	h[i] = s.nextInt();
        }
        System.out.println(Frog2(n,k,h));
        
    }
}