import java.util.*

fun main(args: Array<String>) {
    val scanner = Scanner(System.`in`)
    val n = scanner.nextInt()
    val k = scanner.nextInt()
    val hs = ArrayList<Int>()
    for (i in 1..n) {
        hs.add(scanner.nextInt())
    }
    hs.sort()
    var min = Int.MAX_VALUE
    for (i in 0..(hs.size - k)) {
        val it = hs[i + k - 1] - hs[i]
        if (it < min) min = it
    }
    print(min)
}