object Main extends App {
    val Array(n, k) = io.StdIn.readLine.split(" ").map(_.toInt)
    val hs = for (_ <- 1 to n) yield io.StdIn.readInt

    val sorted = hs.sorted
    val tree_sets = sorted.sliding(k)
    val diffs = tree_sets.map(t => t.max-t.min)
    val result = diffs.min
    println(result)
}