fun main(args: Array<String>) {

    val (n, k) = readLine()!!.split(" ").map(String::toInt).toTypedArray()
    val hn = Array(n) { readLine()!!.toInt() }.sortedArray()

    var result = Integer.MAX_VALUE
    for (i in 0 .. hn.size - k) {
        result = Math.min(result, hn[i + k - 1] - hn[i])
    }
    println(result)


}