#include using namespace std; int main() { int64_t N, M; cin >> N >> M; vector> D(N, vector(M)); for (int64_t i = 0; i < N; i++) { for (int64_t j = 0; j < M; j++) { cin >> D[i][j]; } sort(D[i].begin(), D[i].end()); } // -1であるかどうか判定 int64_t pre_value = 0; for (int64_t i = 0; i < N; i++) { auto itr = lower_bound(D[i].begin(), D[i].end(), pre_value); if (itr == D[i].end()) { cout << -1 << endl; return 0; } pre_value = *itr; } // 二分探索 constexpr int64_t kInf = LLONG_MAX / 2; int64_t ok = kInf / 2, ng = -1; while (ok - ng != 1) { const int64_t mid = (ok + ng) / 2; set st; for (int64_t j = 0; j < M; j++) { st.insert(D[0][j]); } for (int64_t i = 1; i < N; i++) { set new_st; for (int64_t j = 0; j < M; j++) { auto itr = st.lower_bound(D[i][j] - mid); if (itr != st.end() && *itr <= D[i][j]) { new_st.insert(*itr); } } st = new_st; } (st.empty() ? ng = mid : ok = mid); } cout << ok << endl; }