#ifdef NACHIA #define _GLIBCXX_DEBUG #else #define NDEBUG #endif #include #include #include #include namespace nachia{ template< class S, S op(S l, S r) > struct Segtree { private: int N; std::vector A; int xN; void mergev(int i){ if(i < N) A[i] = op(A[i*2], A[i*2+1]); } template int minLeft2(int r, E cmp, int a = 0, int b = 0, int i = -1) const { static S x; if(i == -1){ a=0; b=N; i=1; x=A[0]; } if(r <= a) return a; if(b <= r){ S nx = op(A[i], x); if(cmp(nx)){ x = nx; return a; } } if(b - a == 1) return b; int q = minLeft2(r, cmp, (a+b)/2, b, i*2+1); if(q > (a+b)/2) return q; return minLeft2(r, cmp, a, (a+b)/2, i*2); } template int maxRight2(int l, E cmp, int a = 0, int b = 0, int i = -1) const { static S x; if(i == -1){ a=0; b=N; i=1; x=A[0]; } if(b <= l) return b; if(l <= a){ S nx = op(x, A[i]); if(cmp(nx)){ x = nx; return b; } } if(b - a == 1) return a; int q = maxRight2(l, cmp, a, (a+b)/2, i*2); if(q < (a+b)/2) return q; return maxRight2(l, cmp, (a+b)/2, b, i*2+1); } public: Segtree() : N(0) {} Segtree(int n, S e) : xN(n) { N = 1; while (N < n) N *= 2; A.assign(N * 2, e); } Segtree(const std::vector& a, S e) : Segtree(a.size(), e){ for(int i=0; i<(int)a.size(); i++) A[i + N] = a[i]; for(int i=N-1; i>=1; i--) mergev(i); } S getE() const { return A[0]; } void set(int p, S x){ p += N; A[p] = x; for(int d=1; (1<>d); } S get(int p) const { return A[N+p]; } S prod(int l, int r) const { l += N; r += N; S ql = A[0], qr = A[0]; while(l int minLeft(int r, E cmp) const { return minLeft2(r, cmp); } // bool cmp(S) template int maxRight(int l, E cmp) const { int x = maxRight2(l, cmp); return x > xN ? xN : x; } }; } // namespace nachia #include #include namespace nachia { template> struct PointSetRangeMin{ private: static T minop(T l, T r){ return std::min(l, r, Cmp()); } using Base = Segtree; Base base; Cmp cmpx; public: PointSetRangeMin() {} PointSetRangeMin(int len, T INF) : base(len, INF){} PointSetRangeMin(const std::vector& init, T INF) : base(init, INF){} T min(int l, int r){ return base.prod(l, r); } T min(){ return base.allProd(); } void set(int pos, T val){ base.set(pos, val); } T getinf() const { return base.getE(); } void setinf(int pos){ set(pos, getinf()); } T get(int pos){ return base.get(pos); } void chmin(int pos, T val){ base.set(pos, minop(get(pos), val)); } int lBoundLeft(int from, T val){ return base.minLeft(from, [this,val](const T& x){ return cmpx(val, x); }); } int uBoundLeft(int from, T val){ return base.minLeft(from, [this,val](const T& x){ return !cmpx(x, val); }); } int lBoundRight(int from, T val){ return base.maxRight(from, [this,val](const T& x){ return cmpx(val, x); }); } int uBoundRight(int from, T val){ return base.maxRight(from, [this,val](const T& x){ return !cmpx(x, val); }); } template int minLeft(int r, E cmp){ return base.minLeft(r, cmp); } template int maxRight(int l, E cmp){ return base.maxRight(l, cmp); } int argmin(int l, int r){ auto v = this->min(l, r); if(!cmpx(v, getinf())) return -1; return lBoundRight(l, v); } }; } // namespace nachia using i64 = long long; using u64 = unsigned long long; #define rep(i,n) for(i64 i=0; i void chmin(A& l, const A& r){ if(r < l) l = r; } template void chmax(A& l, const A& r){ if(l < r) l = r; } using namespace std; void testcase(){ i64 H, W; cin >> H >> W; vector A(H); rep(y,H) cin >> A[y]; vector> nx; rep(a,H) rep(b,H) if(a < b){ i64 fl = 0; rep(i,W){ if(A[a][i] < A[b][i]) fl |= 1; if(A[a][i] > A[b][i]) fl |= 2; } if(fl == 3) continue; if(fl == 2) nx.push_back({a,b}); else nx.push_back({b,a}); } vector dp(H, H-1); rep(i,H){ vector nxdp(H, H-1); for(auto [a,b] : nx) chmin(nxdp[b], dp[a] - 1); swap(dp, nxdp); } cout << *min_element(dp.begin(), dp.end()) << "\n"; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); rep(t,1) testcase(); return 0; }