import scala.io.StdIn.readLine

object Main {

  def main(args: Array[String]): Unit = {

    val Array(n, k) = readLine().split(' ').map(_.toInt)
    val hs: Vector[Long] = (1 to n).map(_ => readLine().toLong).toVector.sorted
    println(solve(n, k, hs))

  }

  def solve(n: Int, k: Int, hs: Vector[Long]): Long = {

    (0 to (n - k)).foldLeft(hs(n - 1) - hs(0))((acc, i) =>
      acc min (hs(i + k - 1) - hs(i)))

  }

}
