import Control.Monad
import Data.List

f :: [Int] -> [Int] -> Int
f [h1] [h2] = h1 - h2
f tails heads
  | diff == 0 = 0
  | otherwise = min diff (f (tail tails) (tail heads))
  where diff = (head tails) - (head heads)


main :: IO ()
main = do
  [all, number] <- map read . words <$> getLine
  heights <- replicateM all readLn
  let sorted = sort heights
  let heads = take (all - number + 1) sorted
  let tails = drop (number - 1) sorted
  print $ f tails heads