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

    var ans = Long.MAX_VALUE
    for ((i, hi) in hx.withIndex()) {
        if (hx.size < i + k) {
            break
        }

        val hmax = hx[i + k - 1]
        val hmin = hx[i]
        ans = Math.min(ans, hmax - hmin)
    }

    println(ans)
}