import scala.collection.mutable.ListBuffer

object Main extends App {
  import scala.io.StdIn.readLine

  val aaa = readLine()
  val nks = aaa.split(" ").map(_.toInt)
  val n = nks(0)
  val k = nks(1)

  var list = new ListBuffer[Long]
  for(i <- 1 to n) {
    list += readLine().toLong
  }

  val sortlist = list.sortWith(_ < _)
  var res: Long = 100000000000L;
  for (j <- 0 to n - k) {
    var tmp = sortlist(j + k - 1) - sortlist(j)
    if (tmp < res) res = tmp
  }

  println(res)
}
