import Control.Monad
import Data.List

minDiff :: [Int] -> Int -> Int
minDiff heights number
  | diff == 0                = 0
  | length heights == number = diff
  | otherwise                = min diff minDiffOfTails
  where extracted = take number heights
        diff = (last extracted) - (head extracted)
        minDiffOfTails = minDiff (tail heights) number

main :: IO ()
main = do
  [all, number] <- map read . words <$> getLine
  heights <- replicateM all readLn
  print $ minDiff (sort heights) number