import java.util.Scanner

object ChristmasEve 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).sortBy(v => -v)
  println(trees.sliding(k).map(item => item.head - item.last).min)
}
