#include #include #define eb emplace_back #define all(a) (a).begin(),(a).end() #define RD(T,...) T __VA_ARGS__;li(__VA_ARGS__) #define LL(...) RD(ll,__VA_ARGS__) #define VVL(n,m,...) vvl __VA_ARGS__;setsize({n,m},__VA_ARGS__);li(__VA_ARGS__) #define JO(a,b) a##b #define jo(a,b) JO(a,b) #define uq_symbol(a) jo(a,__LINE__) #define FE(v,e,...) for(au&&__VA_OPT__([)e __VA_OPT__(,__VA_ARGS__]):v) #define FO(n) for(ll uq_symbol(i)=n;uq_symbol(i)-->0;) #define FOR(i,...) for(au[i,i##O,i##E]=rng(0,__VA_ARGS__);i=i##O;i-=i##E) #define debug(...) pp(#__VA_ARGS__,__VA_ARGS__) #define I template #define J class #define au auto #define rr return using std::cin,std::cout,std::begin,std::end,std::rbegin; using std::swap,std::move,std::abs,std::prev,std::next; using std::tuple,std::array,std::bitset,std::minmax,std::get; using vo=void;using bo=bool; vo solve(); Iconstexpr bo same=std::is_same_v; using is=std::istream;using os=std::ostream; using dd=long double;using ll=long long;using ull=unsigned long long; using lll=__int128_t;using ulll=__uint128_t;using str=std::string; using namespace atcoder;using ml=modint;au&operator<<(os&o,const ml&x){rr o<bo in(T l,U m,V r){rr l<=m&&mT zz(T x){rr x<0?-x:x;} Ill len(const T&a){rr a.size();} Iau max(const A&...a){rr max(std::initializer_list>{a...});} struct edg{ ll t,w; edg(){} edg(ll t,ll w=1):t(t),w(w){} friend os&operator<<(os&o,const edg&e){rr o<struct cp{ A a={};B b={}; cp(){} cp(A a,B b):a(a),b(b){} cp(std::pairp):a(p.first),b(p.second){} bo operator==(const cp&c)const{rr a==c.a&&b==c.b;} bo operator<(const cp&c)const{rr a!=c.a?a(const cp&c)const{rr a!=c.a?a>c.a:b>c.b;} friend is&operator>>(is&i,cp&c){rr i>>c.a>>c.b;} friend os&operator<<(os&o,const cp&c){rr o<; Istruct tr{ A a={};B b={};C c={}; tr(){} tr(A a,B b,C c):a(a),b(b),c(c){} bo operator==(const tr&t)const{rr a==t.a&&b==t.b&&c==t.c;} au operator<(const tr&t)const{rr a!=t.a?a>(is&i,tr&t){rr i>>t.a>>t.b>>t.c;} friend os&operator<<(os&o,const tr&t){rr o<; IT&srt(T&a){sort(all(a));rr a;} IT&rv(T&a){reverse(all(a));rr a;} Idecltype(au)bg(T&a){rr*begin(a);} Idecltype(au)rb(T&a){rr*rbegin(a);} Iau pof(T&a){au r=bg(a);a.pop_front();rr r;} Iau pob(T&a){au r=rb(a);a.pop_back();rr r;} Iau pot(T&a){au r=a.top();a.pop();rr r;} Iusing map=std::map; Ios&operator<<(os&o,const map&m){fe(m,e)o<using umap=std::unordered_map; Ios&operator<<(os&o,const umap&m){fe(m,e)o<struct set:std::set{ set(const std::vector&a={}){fe(a,e)this->emplace(e);} friend os&operator<<(os&o,const set&s){fe(s,e)o<struct mset:std::multiset{ mset(const std::vector&a={}){fe(a,e)this->emplace(e);} friend os&operator<<(os&o,const mset&s){fe(s,e)o<T pof(set&s){T r=bg(s);s.erase(s.begin());rr r;} IT pob(set&s){T r=rb(s);s.erase(prev(s.end()));rr r;} IT pof(mset&s){T r=bg(s);s.erase(s.begin());rr r;} IT pob(mset&s){T r=rb(s);s.erase(prev(s.end()));rr r;} Istruct ve; Iconstexpr bo isv=0; Iconstexpr bo isv> =1; Iconstexpr bo isv> =1; Iau rawv(V){if constexpr(isv)rr rawv(V(1)[0]);else rr V();} Istruct ve:std::vector{ using std::vector::vector; using T=decltype(rawv(V())); Ive(const std::vector&v={}){fe(v,e)this->eb(e);} ve&operator+=(const ve&u){au&v=*this;fo(i,len(v))v[i]+=u[i];rr v;} ve&operator-=(const ve&u){au&v=*this;fo(i,len(v))v[i]-=u[i];rr v;} ve&operator^=(const ve&u){fe(u,e)this->eb(e);rr*this;} ve&operator+=(const T&x){au&v=*this;fe(v,e)e+=x;rr v;} ve&operator-=(const T&x){au&v=*this;fe(v,e)e-=x;rr v;} ve&operator*=(const T&x){au&v=*this;fe(v,e)e*=x;rr v;} Iau&operator+=(const bitset&a){fo(i,n)(*this)[i]+=a[i];rr*this;} Iau&operator-=(const bitset&a){fo(i,n)(*this)[i]-=a[i];rr*this;} ve operator+(const ve&u)const{rr ve(*this)+=u;} ve operator-(const ve&u)const{rr ve(*this)-=u;} ve operator^(const ve&u)const{rr ve(*this)^=u;} ve operator+(const T&x)const{rr ve(*this)+=x;} ve operator-(const T&x)const{rr ve(*this)-=x;} ve operator*(const T&x)const{rr ve(*this)*=x;} ve&operator++(){rr*this+=1;} ve&operator--(){rr*this-=1;} ve operator-()const{rr ve(*this)*=-1;} au lower_bound(const V&x)const{rr std::lower_bound(all(*this),x);} au upper_bound(const V&x)const{rr std::upper_bound(all(*this),x);} Iau scan(F f)const{cpr;fe(*this,e)if constexpr(!isv)r.b?f(r.a,e),r:r={e,1};else if(au s=e.scan(f);s.b)r.b?f(r.a,s.a),r:r=s;rr r;} T max()const{rr scan([](T&a,const T&b){a)e.fill(x);else e=x;} }; Iau vec(const ll(&s)[n],T x={}){ if constexpr(n==i+1)rr ve(s[i],x); else{au X=vec(s,x);rr ve(s[i],X);} } Ivo setsize(const ll(&l)[n],A&...a){((a=vec(l,rawv(a))),...);} Iusing vve=ve>;using vl=ve;using vvl=vve; using gl=ve>;using ge=ve>; struct io{io(){cin.tie(0)->sync_with_stdio(0); cout<os&operator<<(os&o,const bitset&b){fo(i,n)o<os&operator<<(os&o,const tuple&t){ apply([&](const au&...a){ll i=0;(((o<os&operator<<(os&o,const std::vector&v){fe(v,e)o<?nl:sp);rr o;} Ivo pp(const A&...a){ll i=0;((cout<is&operator>>(is&i,std::vector&v){fe(v,e)i>>e;rr i;} Ivo li(A&...a){(cin>>...>>a);} Ill bsl(F f,ll o,ll n){while(zz(o-n)>1)(f((o+n)/2)?o:n)=(o+n)/2;rr o;} Istruct rec:F{rec(F&&f):F(std::forward(f)){}Idecltype(au)operator()(A&&...a)const{rr F::operator()(*this,std::forward(a)...);}}; templatestruct swag{ vepre,pre_row,suf,suf_row; swag(const ve&a={}){ fe(a,x)eb(x); } ll size()const{ return len(pre)+len(suf); } vo eb(S x){ suf.eb(op((len(suf)?rb(suf):e()),x)); suf_row.eb(x); } S pof(){ move(); pob(pre_row); return pob(pre); } S front(){ move(); return rb(pre_row); } S fold(){ if(!size())return e(); if(!len(pre))return rb(suf); if(!len(suf))return rb(pre); return op(rb(pre),rb(suf)); } vo move(){ if(!len(pre)){ while(len(suf)){ pre.eb(op(rb(suf_row),(len(pre)?rb(pre):e()))); pre_row.eb(rb(suf_row)); pob(suf); pob(suf_row); } } } friend os&operator<<(os&o,const swag&s){auto u=s.pre_row,v=s.suf_row;return o<T fill(){ if constexpr(same)return x; else if constexpr(same)return{x,x}; else if constexpr(same)return{x,x,x}; else if constexpr(same>>){T r;fe(r,e)e=x;return r;} assert(0); } templateT omax(T a,T b){return bT omin(T a,T b){return aT osum(T a,T b){return a+b;} int main(){ll T=1;fo(T)solve();} vo solve(){ LL(N,M); VVL(N,M,a); fe(a,e)srt(e); ll ans=bsl([&](ll X){//崖をX以下にできるか vl dp(M,1),nx(M); fo(i,1,N){ swag,fill>sw; ll cu=0; fo(j,M){ while(len(sw)&&a[i][j]-X>sw.front().b)sw.pof(); while(cu