import scala.io.StdIn.readLine

object Main extends App {

  val Array(n, k) = readLine.split(" ").map(_.toInt)
  val hs: List[Int] = (0 until n).map(_ => readLine.toInt).toList

  val sorted = hs.sorted

  val min = sorted.sliding(k).map(ss => ss(k-1) - ss(0)).min

  println(min)

}