#include<bits/stdc++.h>
using namespace std;
int t[1001];
int n;
int s;
int p;
int l=1;
int r=1;
int type;
int ans=INT_MAX;
struct student{
int c;
int h;
}a[1000001];
int cmp(student x,student y){
return x.h<y.h;
}
int main(){
cin>>n;
cin>>s;
p=n*s;
for(int i=1;i<=p;i++){
cin>>a[i].h;
a[i].c=floor((i-1)/s)+1;
}
sort(a+1,a+p+1,cmp);
while(l<=r&&r<=p){
if(type<n){
t[a[r].c]++;
if(t[a[r].c]==1){
type++;
}
r++;
}
else{
ans=min(ans,a[r-1].h-a[l].h);
t[a[l].c]--;
if(t[a[l].c]==0){
type--;
}
l++;
}
}
cout<<ans;
}