#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int(i)=0;i<(n);i++)

int main(){
  int n,k,ans=1e9;cin>>n>>k;
  vector<int> x(n,0);
  REP(i,n) cin>>x.at(i);
  sort(x.begin(),x.end());
  REP(i,n-k+1) ans=min(ans,x.at(i+k-1)-x.at(i));
  cout<<ans;
}