import java.util.Scanner

object Main extends App {
  val sc = new Scanner(System.in)
  val Array(n, k) = sc.nextLine.split(" ").map(_.toInt)
  val trees = List.fill(n)(sc.nextLine.toInt).sorted
  val ans = trees.sliding(k).map(item => item.last - item.head).min
  println(ans)
}