import java.util.*

fun main(args : Array<String>) {
    println(inOutExec(Scanner(System.`in`)))
}

fun inOutExec(scanner: Scanner): String {
    val total = scanner.nextInt()
    val num = scanner.nextInt()
    val trees = IntArray(total)
    repeat(total) {
        trees[it] = scanner.nextInt()
    }

    var result: Int = Int.MAX_VALUE
    trees.sort()
    for (i in trees.indices) {
        if (i + num-1 >= total) {
            break
        }
        val diff = Math.abs(trees[i + num-1] - trees[i])
        if (diff < result) {
            result = diff
        }
    }

    return result.toString()
}
