import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = Integer.parseInt(sc.next());
        int K = Integer.parseInt(sc.next());   
        int[] h = new int[N];
        long[] sum = new long[N];
        
        h[0] = Integer.parseInt(sc.next());
        h[1] = Integer.parseInt(sc.next());
        sum[0] = 0;
        sum[1] = (max(h[0], h[1]) - min(h[0], h[1]));  
        
        if(N > 2) {
            for(int i = 2; i < N; i++) {
                h[i] = Integer.parseInt(sc.next());
                long saitan = 0;
                for(int j = i - K; j < i; j++) {
                    if(j < 0) {
                        j = 0;
                    }
                    if(j == 0 || j == i - K) {
                        saitan = max(h[i], h[j]) - min(h[i], h[j]) + sum[j];
                        continue;
                    }
                    
                    if(max(h[i], h[j]) - min(h[i], h[j]) + sum[j]  < saitan) {
                        saitan = max(h[i], h[j]) - min(h[i], h[j]) + sum[j];
                    }
                }
                sum[i] = saitan;
            }
        }
        System.out.println(sum[N-1]);
    }
    
    public static int max(int a, int b) {
        if(a >= b) {
            return a;
        }else {
            return b;
        }
    }
    
    public static int min(int a, int b) {
        if(a >= b) {
            return b;
        }else {
            return a;
        }
    }
} import java.util.*; util.* java public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = Integer.parseInt(sc.next());
        int K = Integer.parseInt(sc.next());   
        int[] h = new int[N];
        long[] sum = new long[N];
        
        h[0] = Integer.parseInt(sc.next());
        h[1] = Integer.parseInt(sc.next());
        sum[0] = 0;
        sum[1] = (max(h[0], h[1]) - min(h[0], h[1]));  
        
        if(N > 2) {
            for(int i = 2; i < N; i++) {
                h[i] = Integer.parseInt(sc.next());
                long saitan = 0;
                for(int j = i - K; j < i; j++) {
                    if(j < 0) {
                        j = 0;
                    }
                    if(j == 0 || j == i - K) {
                        saitan = max(h[i], h[j]) - min(h[i], h[j]) + sum[j];
                        continue;
                    }
                    
                    if(max(h[i], h[j]) - min(h[i], h[j]) + sum[j]  < saitan) {
                        saitan = max(h[i], h[j]) - min(h[i], h[j]) + sum[j];
                    }
                }
                sum[i] = saitan;
            }
        }
        System.out.println(sum[N-1]);
    }
    
    public static int max(int a, int b) {
        if(a >= b) {
            return a;
        }else {
            return b;
        }
    }
    
    public static int min(int a, int b) {
        if(a >= b) {
            return b;
        }else {
            return a;
        }
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = Integer.parseInt(sc.next());
        int K = Integer.parseInt(sc.next());   
        int[] h = new int[N];
        long[] sum = new long[N];
        
        h[0] = Integer.parseInt(sc.next());
        h[1] = Integer.parseInt(sc.next());
        sum[0] = 0;
        sum[1] = (max(h[0], h[1]) - min(h[0], h[1]));  
        
        if(N > 2) {
            for(int i = 2; i < N; i++) {
                h[i] = Integer.parseInt(sc.next());
                long saitan = 0;
                for(int j = i - K; j < i; j++) {
                    if(j < 0) {
                        j = 0;
                    }
                    if(j == 0 || j == i - K) {
                        saitan = max(h[i], h[j]) - min(h[i], h[j]) + sum[j];
                        continue;
                    }
                    
                    if(max(h[i], h[j]) - min(h[i], h[j]) + sum[j]  < saitan) {
                        saitan = max(h[i], h[j]) - min(h[i], h[j]) + sum[j];
                    }
                }
                sum[i] = saitan;
            }
        }
        System.out.println(sum[N-1]);
    } main {
        Scanner sc = new Scanner(System.in);
        int N = Integer.parseInt(sc.next());
        int K = Integer.parseInt(sc.next());   
        int[] h = new int[N];
        long[] sum = new long[N];
        
        h[0] = Integer.parseInt(sc.next());
        h[1] = Integer.parseInt(sc.next());
        sum[0] = 0;
        sum[1] = (max(h[0], h[1]) - min(h[0], h[1]));  
        
        if(N > 2) {
            for(int i = 2; i < N; i++) {
                h[i] = Integer.parseInt(sc.next());
                long saitan = 0;
                for(int j = i - K; j < i; j++) {
                    if(j < 0) {
                        j = 0;
                    }
                    if(j == 0 || j == i - K) {
                        saitan = max(h[i], h[j]) - min(h[i], h[j]) + sum[j];
                        continue;
                    }
                    
                    if(max(h[i], h[j]) - min(h[i], h[j]) + sum[j]  < saitan) {
                        saitan = max(h[i], h[j]) - min(h[i], h[j]) + sum[j];
                    }
                }
                sum[i] = saitan;
            }
        }
        System.out.println(sum[N-1]);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int N = Integer.parseInt(sc.next()); N Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc int K = Integer.parseInt(sc.next()); K Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc int[] h = new int[N]; h new int[N] N long[] sum = new long[N]; sum new long[N] N h[0] = Integer.parseInt(sc.next()) h[0] h 0 Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc h[1] = Integer.parseInt(sc.next()) h[1] h 1 Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc sum[0] = 0 sum[0] sum 0 0 sum[1] = (max(h[0], h[1]) - min(h[0], h[1])) sum[1] sum 1 (max(h[0], h[1]) - min(h[0], h[1])) max(h[0], h[1]) max h[0] h 0 h[1] h 1 min(h[0], h[1]) min h[0] h 0 h[1] h 1 if(N > 2) {
            for(int i = 2; i < N; i++) {
                h[i] = Integer.parseInt(sc.next());
                long saitan = 0;
                for(int j = i - K; j < i; j++) {
                    if(j < 0) {
                        j = 0;
                    }
                    if(j == 0 || j == i - K) {
                        saitan = max(h[i], h[j]) - min(h[i], h[j]) + sum[j];
                        continue;
                    }
                    
                    if(max(h[i], h[j]) - min(h[i], h[j]) + sum[j]  < saitan) {
                        saitan = max(h[i], h[j]) - min(h[i], h[j]) + sum[j];
                    }
                }
                sum[i] = saitan;
            }
        } N > 2 N 2 {
            for(int i = 2; i < N; i++) {
                h[i] = Integer.parseInt(sc.next());
                long saitan = 0;
                for(int j = i - K; j < i; j++) {
                    if(j < 0) {
                        j = 0;
                    }
                    if(j == 0 || j == i - K) {
                        saitan = max(h[i], h[j]) - min(h[i], h[j]) + sum[j];
                        continue;
                    }
                    
                    if(max(h[i], h[j]) - min(h[i], h[j]) + sum[j]  < saitan) {
                        saitan = max(h[i], h[j]) - min(h[i], h[j]) + sum[j];
                    }
                }
                sum[i] = saitan;
            }
        } for(int i = 2; i < N; i++) {
                h[i] = Integer.parseInt(sc.next());
                long saitan = 0;
                for(int j = i - K; j < i; j++) {
                    if(j < 0) {
                        j = 0;
                    }
                    if(j == 0 || j == i - K) {
                        saitan = max(h[i], h[j]) - min(h[i], h[j]) + sum[j];
                        continue;
                    }
                    
                    if(max(h[i], h[j]) - min(h[i], h[j]) + sum[j]  < saitan) {
                        saitan = max(h[i], h[j]) - min(h[i], h[j]) + sum[j];
                    }
                }
                sum[i] = saitan;
            } int i = 2; int i = 2; i 2 i < N i N i++ i++ i {
                h[i] = Integer.parseInt(sc.next());
                long saitan = 0;
                for(int j = i - K; j < i; j++) {
                    if(j < 0) {
                        j = 0;
                    }
                    if(j == 0 || j == i - K) {
                        saitan = max(h[i], h[j]) - min(h[i], h[j]) + sum[j];
                        continue;
                    }
                    
                    if(max(h[i], h[j]) - min(h[i], h[j]) + sum[j]  < saitan) {
                        saitan = max(h[i], h[j]) - min(h[i], h[j]) + sum[j];
                    }
                }
                sum[i] = saitan;
            } {
                h[i] = Integer.parseInt(sc.next());
                long saitan = 0;
                for(int j = i - K; j < i; j++) {
                    if(j < 0) {
                        j = 0;
                    }
                    if(j == 0 || j == i - K) {
                        saitan = max(h[i], h[j]) - min(h[i], h[j]) + sum[j];
                        continue;
                    }
                    
                    if(max(h[i], h[j]) - min(h[i], h[j]) + sum[j]  < saitan) {
                        saitan = max(h[i], h[j]) - min(h[i], h[j]) + sum[j];
                    }
                }
                sum[i] = saitan;
            } h[i] = Integer.parseInt(sc.next()) h[i] h i Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc long saitan = 0; saitan 0 for(int j = i - K; j < i; j++) {
                    if(j < 0) {
                        j = 0;
                    }
                    if(j == 0 || j == i - K) {
                        saitan = max(h[i], h[j]) - min(h[i], h[j]) + sum[j];
                        continue;
                    }
                    
                    if(max(h[i], h[j]) - min(h[i], h[j]) + sum[j]  < saitan) {
                        saitan = max(h[i], h[j]) - min(h[i], h[j]) + sum[j];
                    }
                } int j = i - K; int j = i - K; j i - K i K j < i j i j++ j++ j {
                    if(j < 0) {
                        j = 0;
                    }
                    if(j == 0 || j == i - K) {
                        saitan = max(h[i], h[j]) - min(h[i], h[j]) + sum[j];
                        continue;
                    }
                    
                    if(max(h[i], h[j]) - min(h[i], h[j]) + sum[j]  < saitan) {
                        saitan = max(h[i], h[j]) - min(h[i], h[j]) + sum[j];
                    }
                } {
                    if(j < 0) {
                        j = 0;
                    }
                    if(j == 0 || j == i - K) {
                        saitan = max(h[i], h[j]) - min(h[i], h[j]) + sum[j];
                        continue;
                    }
                    
                    if(max(h[i], h[j]) - min(h[i], h[j]) + sum[j]  < saitan) {
                        saitan = max(h[i], h[j]) - min(h[i], h[j]) + sum[j];
                    }
                } if(j < 0) {
                        j = 0;
                    } j < 0 j 0 {
                        j = 0;
                    } j = 0 j 0 if(j == 0 || j == i - K) {
                        saitan = max(h[i], h[j]) - min(h[i], h[j]) + sum[j];
                        continue;
                    } j == 0 || j == i - K j == 0 j 0 j == i - K j i - K i K {
                        saitan = max(h[i], h[j]) - min(h[i], h[j]) + sum[j];
                        continue;
                    } saitan = max(h[i], h[j]) - min(h[i], h[j]) + sum[j] saitan max(h[i], h[j]) - min(h[i], h[j]) + sum[j] max(h[i], h[j]) - min(h[i], h[j]) max(h[i], h[j]) max h[i] h i h[j] h j min(h[i], h[j]) min h[i] h i h[j] h j sum[j] sum j continue; if(max(h[i], h[j]) - min(h[i], h[j]) + sum[j]  < saitan) {
                        saitan = max(h[i], h[j]) - min(h[i], h[j]) + sum[j];
                    } max(h[i], h[j]) - min(h[i], h[j]) + sum[j]  < saitan max(h[i], h[j]) - min(h[i], h[j]) + sum[j] max(h[i], h[j]) - min(h[i], h[j]) max(h[i], h[j]) max h[i] h i h[j] h j min(h[i], h[j]) min h[i] h i h[j] h j sum[j] sum j saitan {
                        saitan = max(h[i], h[j]) - min(h[i], h[j]) + sum[j];
                    } saitan = max(h[i], h[j]) - min(h[i], h[j]) + sum[j] saitan max(h[i], h[j]) - min(h[i], h[j]) + sum[j] max(h[i], h[j]) - min(h[i], h[j]) max(h[i], h[j]) max h[i] h i h[j] h j min(h[i], h[j]) min h[i] h i h[j] h j sum[j] sum j sum[i] = saitan sum[i] sum i saitan System.out.println(sum[N-1]) System.out.println System.out System System.out sum[N-1] sum N-1 N 1 String[] args args public static int max(int a, int b) {
        if(a >= b) {
            return a;
        }else {
            return b;
        }
    } max {
        if(a >= b) {
            return a;
        }else {
            return b;
        }
    } if(a >= b) {
            return a;
        }else {
            return b;
        } a >= b a b {
            return a;
        } return a; a {
            return b;
        } return b; b int a a int b b public static int min(int a, int b) {
        if(a >= b) {
            return b;
        }else {
            return a;
        }
    } min {
        if(a >= b) {
            return b;
        }else {
            return a;
        }
    } if(a >= b) {
            return b;
        }else {
            return a;
        } a >= b a b {
            return b;
        } return b; b {
            return a;
        } return a; a int a a int b b public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = Integer.parseInt(sc.next());
        int K = Integer.parseInt(sc.next());   
        int[] h = new int[N];
        long[] sum = new long[N];
        
        h[0] = Integer.parseInt(sc.next());
        h[1] = Integer.parseInt(sc.next());
        sum[0] = 0;
        sum[1] = (max(h[0], h[1]) - min(h[0], h[1]));  
        
        if(N > 2) {
            for(int i = 2; i < N; i++) {
                h[i] = Integer.parseInt(sc.next());
                long saitan = 0;
                for(int j = i - K; j < i; j++) {
                    if(j < 0) {
                        j = 0;
                    }
                    if(j == 0 || j == i - K) {
                        saitan = max(h[i], h[j]) - min(h[i], h[j]) + sum[j];
                        continue;
                    }
                    
                    if(max(h[i], h[j]) - min(h[i], h[j]) + sum[j]  < saitan) {
                        saitan = max(h[i], h[j]) - min(h[i], h[j]) + sum[j];
                    }
                }
                sum[i] = saitan;
            }
        }
        System.out.println(sum[N-1]);
    }
    
    public static int max(int a, int b) {
        if(a >= b) {
            return a;
        }else {
            return b;
        }
    }
    
    public static int min(int a, int b) {
        if(a >= b) {
            return b;
        }else {
            return a;
        }
    }
} public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = Integer.parseInt(sc.next());
        int K = Integer.parseInt(sc.next());   
        int[] h = new int[N];
        long[] sum = new long[N];
        
        h[0] = Integer.parseInt(sc.next());
        h[1] = Integer.parseInt(sc.next());
        sum[0] = 0;
        sum[1] = (max(h[0], h[1]) - min(h[0], h[1]));  
        
        if(N > 2) {
            for(int i = 2; i < N; i++) {
                h[i] = Integer.parseInt(sc.next());
                long saitan = 0;
                for(int j = i - K; j < i; j++) {
                    if(j < 0) {
                        j = 0;
                    }
                    if(j == 0 || j == i - K) {
                        saitan = max(h[i], h[j]) - min(h[i], h[j]) + sum[j];
                        continue;
                    }
                    
                    if(max(h[i], h[j]) - min(h[i], h[j]) + sum[j]  < saitan) {
                        saitan = max(h[i], h[j]) - min(h[i], h[j]) + sum[j];
                    }
                }
                sum[i] = saitan;
            }
        }
        System.out.println(sum[N-1]);
    }
    
    public static int max(int a, int b) {
        if(a >= b) {
            return a;
        }else {
            return b;
        }
    }
    
    public static int min(int a, int b) {
        if(a >= b) {
            return b;
        }else {
            return a;
        }
    }
} Main
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = Integer.parseInt(sc.next());
        int K = Integer.parseInt(sc.next());   
        int[] h = new int[N];
        long[] sum = new long[N];
        
        h[0] = Integer.parseInt(sc.next());
        h[1] = Integer.parseInt(sc.next());
        sum[0] = 0;
        sum[1] = (max(h[0], h[1]) - min(h[0], h[1]));  
        
        if(N > 2) {
            for(int i = 2; i < N; i++) {
                h[i] = Integer.parseInt(sc.next());
                long saitan = 0;
                for(int j = i - K; j < i; j++) {
                    if(j < 0) {
                        j = 0;
                    }
                    if(j == 0 || j == i - K) {
                        saitan = max(h[i], h[j]) - min(h[i], h[j]) + sum[j];
                        continue;
                    }
                    
                    if(max(h[i], h[j]) - min(h[i], h[j]) + sum[j]  < saitan) {
                        saitan = max(h[i], h[j]) - min(h[i], h[j]) + sum[j];
                    }
                }
                sum[i] = saitan;
            }
        }
        System.out.println(sum[N-1]);
    }
    
    public static int max(int a, int b) {
        if(a >= b) {
            return a;
        }else {
            return b;
        }
    }
    
    public static int min(int a, int b) {
        if(a >= b) {
            return b;
        }else {
            return a;
        }
    }
}