import Data.List
import Data.Char

differ :: Int -> [Int] -> Int
differ k l = dif (eval c) c (drop k sl) where
    sl = sort l
    c  = newCursor (take k sl) (k-1)
    dif m cl [] 
        | (eval cl) < m = eval cl
        | otherwise     = m
    dif m cl (x:xs)
        | d < m     = dif d (shift x cl) xs
        | otherwise = dif m (shift x cl) xs  
        where
            d  = eval cl

data Cursor a = Cursor [a] [a] deriving (Show)

newCursor :: [a] -> Int -> Cursor a
newCursor l k = Cursor (take k l) (drop k l)
    

shift :: a -> Cursor a -> Cursor a
shift a (Cursor [_] r) = Cursor (reverse r) [a] 
shift a (Cursor (f:fs) r) = Cursor fs (a:r)

eval :: Cursor Int -> Int
eval (Cursor (f:fs) (r:rs)) = r - f 

readData :: Int -> IO [Int]
readData 0 = return []
readData n = do
    stX <- getLine
    let x = read stX :: Int
    fmap (x:) $ readData (n-1)
        
main :: IO() 
main = do
    line1 <- getLine
    let [stN, stK] = words line1
        n = read stN :: Int
        k = read stK :: Int
    l <- readData n
    let ans = differ k l 
    print ans
