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

    var diff = h[k-1] - h[0]
    var min = diff
    for (i in k..h.lastIndex) {
        diff += (h[i]-h[i-1]) - (h[i-k+1]-h[i-k])
        min = Math.min(min, diff)
    }
    min
})