#include #include using namespace std; using namespace atcoder; #define rep(i,n) for(int i=0;i<(n);i++) typedef long long ll; //using mint = modint; using mint = modint998244353; //using mint = modint1000000007; using P = pair; const ll INF=1LL<<60, dx[]={0,1,0,-1},dy[]={1,0,-1,0}; template inline bool chmin(T& a,const T&b) {if(a>b){a=b; return 1;} return 0;} template inline bool chmax(T& a,const T&b) {if(a> n >> m; vector> d(n,vector (m)); rep(i,n) { rep(j,m) cin >> d[i][j]; sort(d[i].begin(),d[i].end()); } auto check=[&](long long k) -> bool { vector> dp(n, vector(m,false)); rep(i,m) dp[0][i]=true; for(int i=1;id[i][t]) t++; while(t= d[i][t]) { dp[i][t]=true; t++; } } } rep(i,m) { if(dp[n-1][i]) return true; } return false; }; ll ans=INF; rep(i,m){ long long left=-1; long long right=2000000000; long long mid=(left+right)/2 +1; while(mid-left>1) { mid=(left+right)/2; if(check(mid)){ right=mid; } else { left=mid; } } chmin(ans,right); } cout << ((ans>1000000000)?-1:ans) << endl; return 0; }