input = require('fs').readFileSync('/dev/stdin', 'utf8')
lines = input.split('\n')
line = lines.shift().split(' ')
N = line[0] * 1
K = line[1] * 1 
lines.pop()
lines = lines.map(x => x * 1).sort((a, b) => a - b)
min = Infinity
for (i=0; i<= N - K; i++) {
    if (min > lines[i + K - 1] - lines[i]) {
        min = lines[i + K - 1] - lines[i]
    }
}
console.log(min)