fun main(args: Array<String>) {
    val (N, K) = readLine()!!.split(" ").map(String::toInt)
    val h = (1..N).map { readLine()!!.toInt() }.sorted()
    print((0..h.size - K).map { h[it + K - 1] - h[it] }.min())
}