#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n,k,a[N],ans;
int main(){
    ios::sync_with_stdio(false);ans=0x3f3f3f3f;
    cin>>n>>k;for(int i=1;i<=n;++i)cin>>a[i];sort(a+1,a+1+n);
    for(int i=1;i<=n-k+1;++i)ans=min(ans,a[i+k-1]-a[i]);
    cout<<ans<<endl;return 0;
}