#include <bits/stdc++.h>
#define endl '\n'
#define left jhghajkhja
#define right oauighgajk
#define prev aioghajga
#define next ioyhjhfajasj
#define y0 iuadoghasdgj
#define y1 taklahgjkla
#define remainder pogjuakllhga
#define pow pajklgaklha
#define pow10 iopuioadjlgkah
#define div aljghajkghak
#define distance gkuftgjasgfjh
#define uppercase ifyhasjkhakjfas
#define count sajhfkajfa
#define rank asfhujfasj
//#define floor hjakjhaja
//#define time ashjlahjka
//#define double_t double
//#define tm kahjflahaajk
using namespace std;
const int N = 1024;
const int INF = (1e9) + 7;
int n,m,a[N][N],arr[N*N],sz,ptr[N],ans;
struct cmp {
bool operator ()(const int &x, const int &y) const {
return a[x][ptr[x]]>a[y][ptr[y]];
}
};
priority_queue < int, vector < int >, cmp > q;
int curr,max_value;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//freopen("test.txt","r",stdin);
//freopen(IN.c_str(),"r",stdin);
//freopen(OUT.c_str(),"w",stdout);
int i,j;
scanf("%d %d", &n, &m);
for(i=1;i<=n;i++) {
for(j=1;j<=m;j++) {
scanf("%d", &a[i][j]);
arr[++sz]=a[i][j];
}
ptr[i]=1;
sort(a[i]+1,a[i]+1+m);
a[i][m+1]=INF;
q.push(i);
max_value=max(max_value,a[i][ptr[i]]);
}
sort(arr+1,arr+1+sz);
ans=INF;
for(i=1;i<=sz;i++) {
if(max_value>arr[i]) continue;
while(true) {
curr=q.top();
if(a[curr][ptr[curr]+1]>arr[i]) break;
q.pop();
++ptr[curr];
max_value=max(max_value,a[curr][ptr[curr]]);
q.push(curr);
}
ans=min(ans,arr[i]-a[q.top()][ptr[q.top()]]);
}
printf("%d\n", ans);
//fprintf(stderr, "Time: %d ms\n", (int)(clock()*1000.0/CLOCKS_PER_SEC));
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CgojZGVmaW5lIGVuZGwgJ1xuJwojZGVmaW5lIGxlZnQgamhnaGFqa2hqYQojZGVmaW5lIHJpZ2h0IG9hdWlna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