import Data.List

subMaxMin a = maximum a - minimum a

solve n as@(_:xs) 
  | n > length as = 100000000
  | otherwise     = let f = subMaxMin . take n
                     in min (f as) (solve n xs)

main :: IO ()
main = do
    [n,k] <- map read . words <$> getLine :: IO [Int]
    h <- map read . lines <$> getContents :: IO [Int]
    print . solve k $ sort h
