import qualified Data.ByteString.Char8 as B
import Data.List
import qualified Data.Vector.Unboxed as UV

a#b = abs$a-b

main = do
    [n,k] <- map read.words<$>getLine
    h <- UV.unfoldr(B.readInt.B.dropWhile(<'!'))<$>B.getLine
    print $ f n k h

inf = (maxBound::Int) `div` 2
f n k h=UV.head $ snd $ UV.foldl' proc (1, UV.cons 0 $ UV.replicate (k-1) inf) (UV.tail h) where
    proc (i,v0) ch = (i+1, UV.cons x $ UV.init v0)
        where
            x = minimum $ zipWith (\a b-> (ch # (h UV.! a))+(v0 UV.! b)) [(i-1),(i-2)..(max 0 (i-k))] [0..]
