import qualified Data.Vector.Unboxed as VU
import Control.Monad
import Data.List

solve :: VU.Vector Int -> Int
solve vec k
  | VU.length vec = 10^9
  | otherwise = min (l-h) $ solve (VU.tail vec)
  where
    h = VU.head ts
    l = VU.last ts
    ts = VU.take k vec

main = do
  [n,k] <- map read . words <$> getLine :: IO [Int]
  hs <- fmap VU.fromList $ fmap sort $ replicateM n readLn :: IO (VU.Vector Int)
  print $ solve (VU.drop (k-1) hs)
