fun main(args : Array<String>) {
    val (N, K) = readLine()!!.split(" ").map { it.toInt() }
    val h = mutableListOf<Long>()
    (1..N).forEach {
        val num = readLine()!!.toLong()
        h.add(num)
    }

    val lastIndex = N - 1
    h.sort()
    val ans = (0..(lastIndex - K + 1)).map {
        val max = h[it + K - 1]
        val min = h[it]
        max - min
    }.min()

    println(ans)
}