(defn input []
  (let [[N K] (->> (clojure.string/split (read-line) #"\s")
                (map #(Integer/parseInt %)))]
   (letfn [(for-input [] (Integer/parseInt (read-line)))]
     (list N K (repeatedly N for-input)))))

(defn Christmas [[N K H]]
  (->> H
    sort
    (partition K 1)
    (map (fn [[f _ l]] (- l f)))
    (apply min)))


(->> (input)
  Christmas
  println)