import scala.io.StdIn.readLine

object MainA 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.toArray

  var min = 2147483647

  (0 to (n - k)).foreach { i =>
    val sa = sorted(i + k - 1) - sorted(i)
    if (sa < min) min = sa
  }

  println(min)

}