/* #include void self::main(){ LL(N,M); VVL(N,M,a); fo(i,N)sort(a[i]); ll D=binary_search_first_true(-1,(int)oo,[&](ll D){ vec dp(N+1,M,int{}); fo(j,M)dp[1][j]=1; fo(i,1,N){ SegMaxs(dp[i]); fo(j,M){ ll l=a[i-1].first_ge(a[i][j]-D).dist(); ll r=a[i-1].first_gt(a[i][j]).dist(); dp[i+1][j]=s.prod(l,r); } } return dp[N].max()>0; }); pp(D==(int)oo?-1:D); } */ #include using namespace std; namespace self{void main();void sub();void pp(auto&&...a);void ppe(auto&&...a);} int main(){cin.tie(nullptr)->sync_with_stdio(0);cout<>__VA_ARGS__;fe(refs(__VA_ARGS__),e)e.get().resizes(n,m);lin(__VA_ARGS__) #define VVL(n,m,...) RDVVL(ll,n,m,__VA_ARGS__) #define fo(i,...) for(auto[i,i##stop,i##step]=for_range(0,__VA_ARGS__);i(1,__VA_ARGS__);i>=i##stop;i+=i##step) #define fe(a,e,...) for(auto&&__VA_OPT__([)e __VA_OPT__(,__VA_ARGS__]):a) namespace self{ using ll=long long; constexpr auto refs(auto&...a){return array{ref(a)...};} templateconstexpr auto for_range(T s,T b){T a=0;if(s)swap(a,b);return array{a-s,b,1-s*2};} templateconstexpr auto for_range(T s,T a,T b,T c=1){return array{a-s,b,(1-s*2)*c};} void lin(auto&...a){(cin>>...>>a);} constexpr ll leading_zero64(ll x){return __builtin_clzll(x);} constexpr ll trailing_zero64(ll x){return __builtin_ctzll(x);} constexpr ll most_significant_bit(ll x){return x?63-leading_zero64(x):-1;} constexpr ll size2(auto x){return most_significant_bit(x|1)+1;} constexpr ll size10(auto x){x|=1;ll r=0;while(x>0)x/=10,++r;return r;} templateconstexpr ll maxsize10(){return size10(numeric_limits::max());} templateT pow2(ll n){return T(1)<T gemin_pow2(T x){if(x<=1)return 1;return pow2(size2(x-1));} constexpr ll log2_floor(auto x){return size2(x)-1;} auto max(auto...a){return max(initializer_list>{a...});} auto min(auto...a){return min(initializer_list>{a...});} ll binary_search_first_true(ll l,ll r,auto&&f){while(l+1struct pair{ A a;B b; pair()=default; pair(A a,B b):a(a),b(b){} auto operator<=>(const pair&)const=default; }; templatestruct infinity{ templatestatic constexpr T ones(size_t n){return n?ones(n-1)*10+1:0;} templateconstexpr operator T()const{return ones(maxsize10())*(1-is_negative*2);} constexpr auto operator-()const{return infinity();} }; constexpr infinity oo; template>decltype(auto)sort(auto&&a,F f={}){ranges::sort(a,f);return a;} templateusing pack_back_t=tuple_element_t>; templateclass Iterator{ private: I it_,begin_,end_; public: Iterator(I it,I begin,I end):it_(it),begin_(begin),end_(end){} ll dist()const{return it_-begin_;} }; } namespace self{ templateconcept vectorial=is_base_of_v::value_type>,remove_cvref_t>; templateistream&operator>>(istream&i,vector&v){fe(v,e)i>>e;return i;} templateconstexpr int depth=0; templateconstexpr int depth =depth+1; templatestruct core_t_helper{using type=T;}; templatestruct core_t_helper{using type=typename core_t_helper::type;}; templateusing core_t=core_t_helper::type; templatestruct vec; templatestruct hvec_helper{using type=vec::type>;}; templatestruct hvec_helper<0,T>{using type=T;}; templateusing hvec=hvec_helper::type; templatestruct vec:vector{ static constexpr int D=depth+1; using C=core_t; using vector::vector; vec(const auto&...a)requires(sizeof...(a)>=3){resizes(a...);} void resizes(const auto&...a){if constexpr(sizeof...(a)==D)*this=make(a...,C{});else*this=make(a...);} static auto make(ll n,const auto&...a){ if constexpr(sizeof...(a)==1)return vec(n,array{a...}[0]); else return vec(n,make(a...)); } auto first_true(const V&x,auto&&f)const{ auto b=this->begin(),e=this->end(); auto p=partition_point(b,e,[&x,&f](const V&v){return !f(x,v);}); return Iterator{p,b,e}; } auto first_ge(const V&x)const{return first_true(x,[](auto&x,auto&e){return x<=e;});} auto first_gt(const V&x)const{return first_true(x,[](auto&x,auto&e){return xvoid for_each_core(F&&f)const{fe(*this,e){if constexpr(vectorial){ }else f(e);}} templateauto fold(auto&&f)const{ pairres{}; for_each_core([&](const C&e){ if(res.b)f(res.a,e); else res={e,1}; }); return res; } auto max()const{return fold([](auto&a,auto b){if(arequires(sizeof...(A)>=2)vec(const A&...a)->vec>>; } namespace self{ templateclass SegTree{ public: ll n_,leaf_size_; vectord_; explicit SegTree(const vector&v):n_(v.size()){ leaf_size_=gemin_pow2(n_); d_=vector(leaf_size_*2,e()); fo(i,n_)d_[i+leaf_size_]=v[i]; of(i,leaf_size_,1)pull(i); } void pull(ll k){d_[k]=op(d_[k*2],d_[k*2+1]);} void for_each_canonical_cover(ll j,ll k,auto&&g)const{ while(j>t); j+=1<struct max_node{ using S=T; static S op(S a,S b){return max(a,b);} static S e(){return-oo;} }; } namespace self{ templateusing SegTreeMonoidAlias=SegTree; templateclass SegMax:public SegTreeMonoidAlias>{ public: using SegTreeMonoidAlias>::SegTreeMonoidAlias; }; } void self::pp(auto&&...a){const char*c="";((cout<s(dp[i]); fo(j,M){ ll l=a[i-1].first_ge(a[i][j]-D).dist(); ll r=a[i-1].first_gt(a[i][j]).dist(); dp[i+1][j]=s.prod(l,r); } } return dp[N].max()>0; }); pp(D==(int)oo?-1:D); }