#include using namespace std; using ll = long long; #ifdef LOCAL #include "algo/debug.h" #else #define debug(...) (void(0)) #endif void run_case(); int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); int T = 1; // cin >> T; while (T--) run_case(); return 0; } void run_case() { 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()); } auto Check = [&](ll x) -> bool { vector dp(M, 1); for(int it = 1; it < N; it++) { vector ndp(M, 0); vector pref(M + 1); for(int j = 0; j < M; j++) pref[j + 1] = pref[j] + dp[j]; int l = 0, r = 0; for(int j = 0; j < M; j++) { while(l < M && D[it - 1][l] < D[it][j] - x) l++; while(r < M && D[it - 1][r] <= D[it][j]) r++; ndp[j] = (pref[r] - pref[l]) > 0; } swap(dp, ndp); } bool ok = false; for(int j = 0; j < M; j++) if(dp[j]) ok = true; return ok; }; if(Check(0)) { cout << 0 << endl; } else { ll ok = 1e10, ng = 0; while(ok - ng > 1) { ll mi = (ok + ng) / 2; (Check(mi) ? ok : ng) = mi; } cout << (ok == (ll)1e10? -1 : ok) << endl; } }