#include<bits/stdc++.h>

using namespace std;
vector<int> yoyo;
int main(){
  int a,b;
  cin>>a>>b;
  for(int i=0;i<a;i++){
    int g;
    cin>>g;
    yoyo.push_back(g);
  }
  sort(yoyo.begin(),yoyo.end());
  int minn=10000000;
  for(int i=0,j=b-1;i<a,j<a;i++,j++){
      if((yoyo[j]-yoyo[i])<minn)minn=yoyo[j]-yoyo[i];
  }
  cout<<minn<<endl;
}