import Data.Array((!), listArray)
main = interact $ show.helper.(map read).words

helper (x:k:xs) = fonk x a 1 k
            where a = listArray (1, x) xs

fonk x a i k = f i
        where 
            f i 
                | x == i = 0
                | x - 1 == i  = abs((a!i) - a!(i+1))
                | otherwise = minn (i+1) i
            minn it fi 
                | it == x || it-fi == k = y
                | otherwise = min y $ minn (it+1) fi
                where y = (ll!it) + abs(a!fi - a!it)
                      ll = listArray (1, x) $ map f [1..x]
