#include #include using namespace std; using namespace atcoder; using ll=long long; using mint=modint998244353; #define rep(i,n) for(int i=0;i=0;i--) #define all(v) v.begin(),v.end() #define rall(v) v.rbegin(),v.rend() template bool chmax(T &a, T b){if (a < b){a = b;return true;} else return false;} template bool chmin(T &a, T b){if (a > b){a = b;return true;} else return false;} int n,m; int main(){ ios::sync_with_stdio(false); std::cin.tie(nullptr); cin>>n>>m; vector>d(n,vector(m)); rep(i,n){ rep(j,m)cin>>d[i][j]; sort(all(d[i])); } int ok=1<<30,ng=-1; while(ok-ng>1){ int mid=(ok+ng)/2; vectordp(m,1); rep(i,n-1){ int l=0,r=0,s=0; bool f=true; vectordp2(m); rep(j,m){ while(r=d[i][r]){ if(dp[r]==1)s++; r++; } while(ld[i][l]+mid){ if(dp[l]==1)s--; l++; } if(s>0)dp2[j]=1; } rep(j,m)swap(dp[j],dp2[j]); } bool flag=false; rep(i,m)flag=flag||(dp[i]>0); if(flag)ok=mid; else ng=mid; } if(ok==1<<30)cout<<-1<<"\n"; else cout<