#include using namespace std; int main() { int n, m; cin >> n >> m; vector d(n, vector(m, 0)); for(auto&&v : d){ for(auto&&e : v) cin >> e; sort(begin(v), end(v)); } auto isok = [&](int x) -> bool { vector ok(m + 1); iota(begin(ok), end(ok), 0); for(int i = 0; i < n - 1; ++i){ vector nx(m + 1); for(int j = 0; j < m; ++j){ int l = lower_bound(begin(d[i]), end(d[i]), d[i+1][j] - x) - begin(d[i]); int r = upper_bound(begin(d[i]), end(d[i]), d[i+1][j]) - begin(d[i]); nx[j + 1] = nx[j] + ((ok[r] - ok[l]) > 0); } ok = nx; } return ok[m] > 0; }; int inf = 1e9 + 1; int ok = inf, ng = -1; while(abs(ok-ng) > 1){ int wj = (ok + ng)/2; if(isok(wj)) ok = wj; else ng = wj; } if(ok == inf) ok = -1; cout << ok << "\n"; }