fun main(args: Array<String>){
    val input: List<String> = readLine()!!.split(" ")
    val num = Integer.parseInt(input[0])
    val k = Integer.parseInt(input[1])
    val trees = mutableListOf<Int>()

    for(i in 1..num){
        trees.add(Integer.parseInt(readLine()))
    }

    val sortedTree = trees.sorted()
    var min = sortedTree[num - 1]

    for(i in 0..(num - k)){
        val tmp = sortedTree[i+k-1] - sortedTree[i]
        if(tmp < min) min = tmp
    }
    println(min)
}