function main(input) {
  var N = input[0].split(" ")[0]-0;
  var K = input[0].split(" ")[1]-0;

  var map = {};
  var maxsame = 0;
  var min = max = input[1];
  for(var i=1;i<=N;i++){
    var t = input[i]-0;
    if(!map[t])map[t] = 1;
    else map[t]++;
    maxsame = Math.max(maxsame, map[t]);
    min = Math.min(t, min);
    max = Math.max(t, max);
  }
  if(maxsame>=K) console.log(0);
  else{
    var ans = max-min;
    var keys = Object.keys(map).sort((a,b)=>a-b);
    start:for(var i=0;i<keys.length;i++){
      var first = keys[i];
      var tmpnum = map[first];
      for(var j=i+1;j<keys.length;j++){
        var last = keys[j];
        if(last-first>ans) continue start;
        tmpnum += map[last];
        if(tmpnum>=K){
          ans = Math.min(last-first, ans);
        }
      }
    }
    console.log(ans);
  }
}

main(require('fs').readFileSync('/dev/stdin', 'utf8').trim().split("\n"))
