import Data.List
import Data.Maybe
import Control.Monad
import qualified Data.Vector.Unboxed as V
import qualified Data.ByteString.Char8 as BS

readInt' :: BS.ByteString -> Int
readInt' bs = (fst.fromJust.BS.readInt) bs

main :: IO ()
main = do
  [n,k] <- (map readInt'). BS.words <$> BS.getLine
  hs <- V.fromList . sort <$> (replicateM n $ readInt' <$> BS.getLine)
  print $ minimum [ (hs V.! (i+k-1)) - (hs V.! i) | i <- [0..n-k]]