#include using namespace std; #define INF 1000000001 int main() { 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()); } int ng = -1, ok = INF; while (ok - ng > 1) { int key = (ok + ng) / 2; vector dp(m, true); for (int i = 0; i < n - 1; i++) { vector ndp(m, false); int l = 0; for (int j = 0; j < m; j++) { if (!dp[j]) continue; while (l < m && d[i + 1][l] < d[i][j]) { l++; } while (l < m && d[i][j] <= d[i + 1][l] && d[i + 1][l] <= d[i][j] + key) { ndp[l] = true; l++; } } dp.swap(ndp); } bool flag = false; for (int i = 0; i < m; i++) { if (dp[i]) { flag = true; break; } } if (flag) ok = key; else ng = key; } cout << (ok == INF ? -1 : ok) << endl; }