function main(input) {
    const args = input.split(' ');
    const N = parseInt(args[0], 10);
    const K = parseInt(args[1], 10);
    const TREES = input.split('\n');

    var trees = [];

    for(var i = 1; i < N + 1; i++) {
        trees[i] = parseInt(TREES[i]);
    }

    trees.sort();

    var ans = trees[trees.length - 2];
    var temp_ans = 0;
    var temp_tree = [];

    for(var i = 0; i < trees.length - K; i++) {
        temp_tree = trees.slice(i, i + K);
        console.log(temp_tree);
        temp_ans = Math.max.apply(null, temp_tree) - Math.min.apply(null, temp_tree);
        if(temp_ans < ans) {
            ans = temp_ans;
        }
    }

    console.log(ans);
}

main(require('fs').readFileSync('/dev/stdin', 'utf8'));

