#include #include using namespace std; //#define DISABLE_PRINT #if defined(ENABLE_PRINT) && !defined(DISABLE_PRINT) #define P(...) fprintf(stderr, __VA_ARGS__) #define LP fprintf(stderr, "L: %d\n", __LINE__) #else #define P(...) ((void)0) #define LP ((void)0) #endif #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define ALL(x) x.begin(), x.end() using ll = long long; using ull = unsigned long long; const int INF = 1100100100; const ll INFLL = 1100100100100100100; int main() { int N, M; cin >> N >> M; vector> D(N, vector(M)); rep(i, N) { rep(j, M) { cin >> D[i][j]; } sort(ALL(D[i])); } auto test = [&](ll x) { P("x: %lld\n", x); function f = [&](ll l, ll r, int di) { P("ll: %lld, rr: %lld, di: %d\n", l, r, di); if (di == N) return true; auto ll = l + x; auto rr = r + x; auto fl = upper_bound(ALL(D[di]), ll); auto fr = upper_bound(ALL(D[di]), rr); if (fl == D[di].begin()) return false; if (fr == D[di].begin()) return false; fl--; fr--; if (*fl < l || *fr < r) return false; return f(*fl, *fr, di + 1); }; deque s; int l = 0; for (int r = 1; r <= N; ++r) { if (r == N || D[0][r] - D[0][l] > x) { auto result = f(D[0][l], D[0][r - 1], 1); if (result) return true; } if (r == N) break; while (D[0][r] - D[0][l] > x) { l++; } } return false; }; ll ng = -1, ok = INFLL; // ll ng = -1, ok = 1; while (ok - ng != 1) { auto m = (ok + ng) / 2; if (test(m)) { ok = m; } else ng = m; } cout << (ok == INFLL ? -1 : ok) << endl; return 0; }