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