1748. Chronosphere Hacker

Once Hefeng peeped Yunyan's Abyss Log, and thus gained the ability of keeping constant memory. Since then even if Yunyan went back tens of thousands of times, she could always feel it. Different from Yunyan's great intension to overturn Tianjiang's world, Hefeng prefers a peace way such as negotiation. Yunyan refused Hefeng's suggestions, but she didn't want Yunyan to suffer such endless torment, so she had no choice but hacked the Chronosphere in order to give Yunyan a relief from the infinite time loop. After her hack, the Chronosphere would disappeared immediately after being used. Thereby when Yunyan used the Chronosphere once more, he found it lost. At the same time, backing to the past as well, Hefeng started her saving plans furtively.

The Chronosphere's inner structure is a matrix (aij)n×m(a_{ij})_{n\times m} with nn rows and mm columns. Each element ai,ja_{i,j} is an integer related to time and space. Define mex(s)mex(s) as the minimal non-negative integer that hasn't appear in set ss . Specially, mex()=0mex(\varnothing)=0 . Then 1in\forall 1\le i\le n , let bi=mex(ai)b_i=mex(a_i) where aia_i is a matrix row i.e. ai=(ai,1,ai,2,,ai,m)a_i=(a_{i,1},a_{i,2},\cdots, a_{i,m}) . And define the energy of the Chronosphere as mex(b)=mex(b1,b2,,bn)mex(b)=mex(b_1,b_2,\cdots, b_n) .

To make the hack success, Hefeng needs to delete some rows from the matrix's head and tail while not changing the energy of the Chronosphere. And the less rows left, the better. In other word, let origin energy as e0=mex(b)e_0=mex(b) , and you can delete the first xx rows and the last yy rows, such that 0x,y,x+yn0\le x,y,x+y\le n . If x=0x=0 or y=0y=0 it means you choose not to delete the head or tail. After the delete, let new energy e1=mex(bx,bx+1,,bny)e_1=mex(b_x,b_{x+1},\cdots, b_{n-y}) , you should find the minimal n(x+y)n-(x+y) satisfying e1=e0e_1=e_0 .

输入

Input the first line with two integers n,m(1n,m,n×m106)n,m(1\le n,m,n\times m\le10^6) .

Then input nn lines, each line contains mm integers. The i-th line's j-th integer is ai,j(0ai,j109)a_{i,j}(0\le a_{i,j}\le10^9) .

输出

Output a single line with one integer, denoting the minimal number of rows left.

样例

标准输入 复制文本
4 3
1 2 3
0 2 3
0 1 3
0 1 2
标准输出 复制文本
4
标准输入 复制文本
6 2
1 2
2 1
0 2
0 1
0 1
0 2
标准输出 复制文本
3
标准输入 复制文本
7 2
1 2
0 2
0 2
0 2
0 1
0 1
1 2
标准输出 复制文本
4

提示

In the first example, b=(0,1,2,3),e0=4b=(0,1,2,3),e_0=4 , no matter how to delete is invalid, so it cannot delete and the minimal number of rows left are 44 rows.

In the second example, b=(0,0,1,2,2,1),e0=3b=(0,0,1,2,2,1),e_0=3 , the best deletion is delete the first 11 row and the last 22 rows, then b=(0,1,2)b'=(0,1,2), so the minimal number of rows left are 33 rows.

In the third example, b=(0,1,1,1,2,2,0),e0=3b=(0,1,1,1,2,2,0),e_0=3 , the best deletion is delete the first 33 row, then b=(1,2,2,0)b'=(1,2,2,0), so the minimal number of rows left are 44 rows.

Note that you may left 00 row (that is delete all the rows) if e0=0e_0=0 .

来源

2022 软件学院 ACM 集训队筛选赛

登录以提交代码。
单点时限 1 秒
内存限制 128 MB
提交 119
通过 28