#include // clang-format off using namespace std; using ll=long long; using ull=unsigned long long; using pll=pair; const ll INF=4e18; void print0(){}; template void print0(H h,T... t){cout<void print(H h,T... t){print0(h);if(sizeof...(T)>0)print0(" ");print(t...);} void perr0(){}; template void perr0(H h,T... t){cerr<void perr(H h,T... t){perr0(h);if(sizeof...(T)>0)perr0(" ");perr(t...);} void ioinit() { cout< dp(M + 1, true); vector ndp(M + 1); for (ll i = 1; i < N; i++) { ll right = -1; for (ll j = 0; j < M; j++) { if (!dp[j]) continue; ll cur = D[i - 1][j]; while (right + 1 < M && D[i][right + 1] < cur) { right++; } while (right + 1 < M && D[i][right + 1] <= cur + diff) { ndp[right + 1] = true; right++; } } swap(dp, ndp); for (ll j = 0; j < M; j++) { ndp[j] = false; } } for (ll j = 0; j < M; j++) { if (dp[j]) return true; } return false; } int main() { ioinit(); cin >> N >> M; for (ll i = 0; i < N; i++) { vector DD; for (ll j = 0; j < M; j++) { ll d; cin >> d; DD.push_back(d); } sort(DD.begin(), DD.end()); for (ll j = 0; j < M; j++) { D[i][j] = DD[j]; } } if (N == 1) { print(0); return 0; } const ll MX = 1000000001; ll left = 0; ll right = MX - 1; ll ans = MX; while (left <= right) { ll mid = (left + right) / 2; if (accept(mid)) { ans = mid; right = mid - 1; } else { left = mid + 1; } } if (ans == MX) ans = -1; print(ans); return 0; }