#include using namespace std; #define rep(i, n) for (int i=0; i> N >> M; vector D[N]; rep(i, N) rep(j, M) { int d; cin >> d; D[i].pb(d); } rep(i, N) sort(D[i].begin(), D[i].end()); int l = 0; int r = 1000000010; while (l<=r) { int m = (l+r)/2; bool dp[M]; rep(i, M) dp[i] = true; for (int i=1; i acc = {0}; rep(j, M) acc.pb(acc.back()+(dp[j] ? 1 : 0)); bool ndp[M]; rep(j, M) ndp[j] = false; rep(j, M) { int le = lower_bound(D[i-1].begin(), D[i-1].end(), D[i][j]-m)-D[i-1].begin(); int ri = upper_bound(D[i-1].begin(), D[i-1].end(), D[i][j])-D[i-1].begin(); if (acc[ri]-acc[le]>0) ndp[j] = true; } rep(j, M) dp[j] = ndp[j]; } bool ok = false; rep(i, M) if (dp[i]) ok = true; if (ok) r = m-1; else l = m+1; } int ans = l; if (ans>1000000010) cout << -1 << endl; else cout << ans << endl; }