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 $(a{ij}){n\times m} with n rows and m columns. Each element a_{i,j} is an integer related to time and space. Define mex(s) as the minimal non-negative integer that hasn't appear in set s . Specially, mex(\varnothing)=0 . Then \forall 1\le i\le n , let b_i=mex(a_i) where a_i is a matrix row i.e. ai=(a{i,1},a{i,2},\cdots, a{i,m}) . And define the energy of the Chronosphere as 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 e_0=mex(b) , and you can delete the first x rows and the last y rows, such that 0\le x,y,x+y\le n . If x=0 or y=0 it means you choose not to delete the head or tail. After the delete, let new energy $e_1=mex(bx,b{x+1},\cdots, b_{n-y}) , you should find the minimal n-(x+y) satisfying e_1=e_0$ .

输入

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

Then input n lines, each line contains m integers. The i-th line's j-th integer is $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),e_0=4 , no matter how to delete is invalid, so it cannot delete and the minimal number of rows left are 4 rows.

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

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

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

来源

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

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