import java.util.*;

class Main {
    public static int frog(int[] h, int n, int k) {
    	int[] dp = new int[n];

    	for(int i = 1; i < n; i++) {
    		dp[i] = dp[i-1] + Math.abs(h[i] - h[i-1]);
    		for(int j = 2; j <= k; j++) {
    			if(j > i) break;
    			dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(h[i] - h[i-j]));
    		}
    	}

    	return dp[n-1];
    }

    public static void main(String[] args) {
    	Scanner sc = new Scanner(System.in);

    	int n = sc.nextInt();
    	int k = sc.nextInt();
    	int[] h = new int[n];
    	for(int i = 0; i < n; i++) {
    		h[i] = sc.nextInt();
    	}

    	System.out.println(frog(h, n, k));
    }
} import java.util.*; util.* java class Main {
    public static int frog(int[] h, int n, int k) {
    	int[] dp = new int[n];

    	for(int i = 1; i < n; i++) {
    		dp[i] = dp[i-1] + Math.abs(h[i] - h[i-1]);
    		for(int j = 2; j <= k; j++) {
    			if(j > i) break;
    			dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(h[i] - h[i-j]));
    		}
    	}

    	return dp[n-1];
    }

    public static void main(String[] args) {
    	Scanner sc = new Scanner(System.in);

    	int n = sc.nextInt();
    	int k = sc.nextInt();
    	int[] h = new int[n];
    	for(int i = 0; i < n; i++) {
    		h[i] = sc.nextInt();
    	}

    	System.out.println(frog(h, n, k));
    }
} Main public static int frog(int[] h, int n, int k) {
    	int[] dp = new int[n];

    	for(int i = 1; i < n; i++) {
    		dp[i] = dp[i-1] + Math.abs(h[i] - h[i-1]);
    		for(int j = 2; j <= k; j++) {
    			if(j > i) break;
    			dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(h[i] - h[i-j]));
    		}
    	}

    	return dp[n-1];
    } frog {
    	int[] dp = new int[n];

    	for(int i = 1; i < n; i++) {
    		dp[i] = dp[i-1] + Math.abs(h[i] - h[i-1]);
    		for(int j = 2; j <= k; j++) {
    			if(j > i) break;
    			dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(h[i] - h[i-j]));
    		}
    	}

    	return dp[n-1];
    } int[] dp = new int[n]; dp new int[n] n for(int i = 1; i < n; i++) {
    		dp[i] = dp[i-1] + Math.abs(h[i] - h[i-1]);
    		for(int j = 2; j <= k; j++) {
    			if(j > i) break;
    			dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(h[i] - h[i-j]));
    		}
    	} int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
    		dp[i] = dp[i-1] + Math.abs(h[i] - h[i-1]);
    		for(int j = 2; j <= k; j++) {
    			if(j > i) break;
    			dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(h[i] - h[i-j]));
    		}
    	} {
    		dp[i] = dp[i-1] + Math.abs(h[i] - h[i-1]);
    		for(int j = 2; j <= k; j++) {
    			if(j > i) break;
    			dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(h[i] - h[i-j]));
    		}
    	} dp[i] = dp[i-1] + Math.abs(h[i] - h[i-1]) dp[i] dp i dp[i-1] + Math.abs(h[i] - h[i-1]) dp[i-1] dp i-1 i 1 Math.abs(h[i] - h[i-1]) Math.abs Math h[i] - h[i-1] h[i] h i h[i-1] h i-1 i 1 for(int j = 2; j <= k; j++) {
    			if(j > i) break;
    			dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(h[i] - h[i-j]));
    		} int j = 2; int j = 2; j 2 j <= k j k j++ j++ j {
    			if(j > i) break;
    			dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(h[i] - h[i-j]));
    		} {
    			if(j > i) break;
    			dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(h[i] - h[i-j]));
    		} if(j > i) break; j > i j i break; dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(h[i] - h[i-j])) dp[i] dp i Math.min(dp[i], dp[i-j] + Math.abs(h[i] - h[i-j])) Math.min Math dp[i] dp i dp[i-j] + Math.abs(h[i] - h[i-j]) dp[i-j] dp i-j i j Math.abs(h[i] - h[i-j]) Math.abs Math h[i] - h[i-j] h[i] h i h[i-j] h i-j i j return dp[n-1]; dp[n-1] dp n-1 n 1 int[] h h int n n int k k public static void main(String[] args) {
    	Scanner sc = new Scanner(System.in);

    	int n = sc.nextInt();
    	int k = sc.nextInt();
    	int[] h = new int[n];
    	for(int i = 0; i < n; i++) {
    		h[i] = sc.nextInt();
    	}

    	System.out.println(frog(h, n, k));
    } main {
    	Scanner sc = new Scanner(System.in);

    	int n = sc.nextInt();
    	int k = sc.nextInt();
    	int[] h = new int[n];
    	for(int i = 0; i < n; i++) {
    		h[i] = sc.nextInt();
    	}

    	System.out.println(frog(h, n, k));
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc int k = sc.nextInt(); k sc.nextInt() sc.nextInt sc int[] h = new int[n]; h new int[n] n for(int i = 0; i < n; i++) {
    		h[i] = sc.nextInt();
    	} int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
    		h[i] = sc.nextInt();
    	} {
    		h[i] = sc.nextInt();
    	} h[i] = sc.nextInt() h[i] h i sc.nextInt() sc.nextInt sc System.out.println(frog(h, n, k)) System.out.println System.out System System.out frog(h, n, k) frog h n k String[] args args
import java.util.*;

class Main {
    public static int frog(int[] h, int n, int k) {
    	int[] dp = new int[n];

    	for(int i = 1; i < n; i++) {
    		dp[i] = dp[i-1] + Math.abs(h[i] - h[i-1]);
    		for(int j = 2; j <= k; j++) {
    			if(j > i) break;
    			dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(h[i] - h[i-j]));
    		}
    	}

    	return dp[n-1];
    }

    public static void main(String[] args) {
    	Scanner sc = new Scanner(System.in);

    	int n = sc.nextInt();
    	int k = sc.nextInt();
    	int[] h = new int[n];
    	for(int i = 0; i < n; i++) {
    		h[i] = sc.nextInt();
    	}

    	System.out.println(frog(h, n, k));
    }
}