#pragma GCC target("avx") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(false); int N, M; cin >> N >> M; constexpr int INF = 1<<30; vector> D(N, vector(M)), dp(N, vector(M, INF)); for( int i = 0; i < N; ++i ) { for( int j = 0; j < M; ++j ) { cin >> D[i][j]; } sort(D[i].begin(), D[i].end()); } for( int j = 0; j < M; ++j ) { dp[0][j] = 0; } for( int i = 0; i < N-1; ++i ) { for( int j = M-1; j >= 0; --j ) { for( int k = M-1; k >= 0 && D[i+1][k]-D[i][j] >= 0; --k ) { if( dp[i][j] == INF ) continue; dp[i+1][k] = min(dp[i+1][k], max(dp[i][j], D[i+1][k]-D[i][j])); } } } int me = *min_element(dp[N-1].begin(), dp[N-1].end()); cout << ( me == INF ? -1 : me ) << endl; }