import scala.io.StdIn.readLine

object Main extends App {
  val Array(n, k) = readLine.split(" ").map(_.toInt)
  val seq = Iterator.continually(readLine)
    .take(n)
    .map(_.toInt)
    .toArray
    .sorted
  val l = k-1
  val ans = (l until n).map(i => seq(i) - seq(i-l))
  // println(ans.mkString(" "))
  println(ans.min)
}