function Main(input) {
    input = input.split("\n");
    var temp = input[0].split(" ");
    var N = temp[0];
    var K = temp[1];
    
    var h = input.slice(1, N+1);
    h.sort((n,m)=> n-m);

    var min = Math.pow(10, 9);
    
    for( var  i=0; i<(N-K)+1 ; i++) {
        var tempMin = h[i+(K-1)] - h[i];
        min = Math.min(min, tempMin);
    }
    
    console.log(min);
}
Main(require("fs").readFileSync("/dev/stdin", "utf8"));