fun main(args: Array<String>) {
    val (n, k) = readLine()!!.split(" ").map(String::toInt)
    val trees = (1..n).map { readLine()!!.toInt() }.sorted()

    var min = trees.max()!! - trees.min()!!
    for (i in 0..(n - k)) {
        val one = trees[i]
        val two = trees[i + k - 1]
        val sub = two - one
        if (sub < min) {
            min = sub
        }
    }
    println(min)
}