#include <bits/stdc++.h>#define int long long
using namespace std ;
struct ren
{
int value , id ;
}a [ 1000100 ] ;
int n , s , sr ;
int t [ 1000100 ] = { 0 } ;
bool cmp ( ren x , ren y )
{
return x . value < y .value ;
}
signed main ()
{
cin >> n >> s ;
int cnt = 0 ;
for ( int i = 1 ; i <= n ;i ++ )
{
for ( int j = 1 ; j <= s ; j ++ )
{
cnt ++ ; //由于cnt初值是0,所以要先加一
cin >> a [ cnt ] . value ;
a [ cnt ] . id = i ;
}
}
sort ( a + 1 , a + 1 + cnt , cmp );
int l = 1 , r = 1 , ans = INT_MAX ; //因为ans要存最小值,所以初值要尽可能大
cnt = 0 ;
while ( l <= r && r <= n * s + 1 )
{
if ( cnt < n )
{
t [ a [ r ] . id ] ++ ;
if ( t [ a [ r ] . id ] == 1 )
{
cnt ++ ;
}
r ++ ;
}
else
{
ans = min ( ans , a [ r - 1 ] . value - a [ l ] . value ) ; //由于按照身高排了序,所以可以直接用最后一个人的体重减去第一个人的体重
t [ a [ l ] . id ] -- ;
if ( ! t [ a [ l ] . id ] )
{
cnt -- ;
}
l ++ ;
}
}
cout << ans ;
}