import io.StdIn._

object Main {
  def main(args: Array[String]): Unit = {
    val Array(n, k) = readLine.split(" ").map(_.toInt)
    val trees = (for {_ <- 1 to n} yield readInt()).sorted
    val ans = (0 to n - k).map(i => trees(i + k - 1) - trees(i)).min
    println(ans)
  }
}