#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; void Main() { int N,M; cin >> N >> M; vector> D(N,vector(M)); 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()); } int ng = -1,ok = (int)1e9; while(ok - ng > 1) { int mid = (ok + ng) / 2; vector dp(M,1); for(int i = 1;i < N;i++) { for(int j = 1;j < M;j++) { dp[j] += dp[j - 1]; } vector n_dp(M); //D[i - 1][k] >= D[i][j] - mid && D[i - k] <= D[i][j] int l = 0,r = 0; for(int j = 0;j < M;j++) { while(r < M && D[i - 1][r] <= D[i][j]) { r++; } while(l < M && D[i - 1][l] < D[i][j] - mid) { l++; } int cur = 0; if(r > 0) { cur += dp[r - 1]; } if(l > 0) { cur -= dp[l - 1]; } if(cur) { n_dp[j] = 1; } } swap(dp,n_dp); } bool check = false; for(int j = 0;j < M;j++) { check |= dp[j]; } if(check) { ok = mid; } else { ng = mid; } } int ans = ok; if(ans == (int)1e9) { ans = -1; } cout << ans << endl; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int tt = 1; /* cin >> tt; */ while(tt--) Main(); }