s=require("fs").readFileSync("/dev/stdin","utf8").split('\n')
n=s[0].split(' ')[0]|0,k=s[0].split(' ')[1]|0
s.shift()
s=s.sort((a,b)=>b-a)
console.log(Math.min(...[...Array(n-k+1)].map((_,i)=>s[i]-s[i+k-1])))
