#include <bits/stdc++.h>
using namespace std;
int a[100001];
int main()
{
	int n,i,k,minn=INT_MAX;
	scanf("%d %d",&n,&k);
	for(i=1;i<=n;i++)
		scanf("%d",&a[i]);
	sort(a+1,a+1+n);
	for(i=1;i<=n-k+1;i++)
		minn=min(minn,a[i+k-1]-a[i]);
	printf("%d",minn);
	return 0;
}
