import Data.List
import Control.Monad
import Control.Applicative

solve hs k
    | length hs < k = 10^10
    | otherwise = min (mx-mn) (solve (tail hs) k)
        where
            ts = take k hs
            mx = maximum ts
            mn = minimum ts

main = do
    [n,k] <- map read . words <$> getLine :: IO [Int]
    hs <- fmap sort $ replicateM n readLn :: IO [Int]
    print $ solve hs k