import java.io.{InputStream, OutputStream, PrintStream}
import java.util.Scanner

object Main {

  def main(args: Array[String]): Unit = {
    val solver = new Solver(System.in, System.out)
    solver.solve()
  }

  class Solver(in: InputStream, out: OutputStream) {
    lazy val input = new Scanner(in)

    lazy val output = new PrintStream(out)

    def solve(): Unit = {
      val Array(n, k) = input.nextLine().split(" ").map(_.toInt)
      val ns          = 1.to(n).toArray.map(_ => input.nextLine().toInt).sorted
      println(
        0.to(n - k)
          .map { i =>
            Math.abs(ns(i) - ns(i + k-1))
          }
          .min
      )
    }

  }

}
