"use strict";
function main(arg) {
    const inputs = arg.split("\n")
    const N = parseInt(inputs[0].split(" ")[0])
    const K = parseInt(inputs[0].split(" ")[1])
    const list = inputs.slice(1,N+1).map((x)=>parseInt(x)).sort()
    let min = Math.pow(10,9)
    let i
    for(i=0; i<N-K+1; i++)
        min = Math.min(min, list[i+K-1] - list[i])
    
    console.log(min)
}
main(require('fs').readFileSync('/dev/stdin', 'utf8'));