Obtain a Permutation

题意翻译

## 题意翻译 给你一个由$1$到$2\times 10^5$的整数组成的$n\times m$矩阵 在一步操作中,你可以: - 选择矩阵中的**任何**一个元素并将其变成$1$到$n\times m$(含)中的**任意**整数 - 选择一列并将其周期性的向上移动一个单元格 周期性的向上移动一个单元格意味着你选择一个$j (1\le j\le m)$并且**同时**进行如下操作:$a_{1, j} := a_{2, j}, a_{2, j} := a_{3, j}, \dots, a_{n, j} := a_{1, j}$ ![](https://espresso.codeforces.com/874a76a681a525102f53f73c046f306bcc2f1b6d.png) 上面是一个例子 你想要用最少的操作次数使得这个矩阵看起来像这样: ![](https://espresso.codeforces.com/886b8c37bc9a14c44d2c736abb7a98b18d00de3d.png) 也就是说,你的目标是用**最少的操作次数**得到这样一个矩阵,使得 $a_{1, 1} = 1, a_{1, 2} = 2, \dots, a_{1, m} = m, a_{2, 1} = m + 1, a_{2, 2} = m + 2, \dots, a_{n, m} = n \cdot m$ ## 数据范围 $1 \le n, m \le 2 \cdot 10^5, n \cdot m \le 2 \cdot 10^5$ $1 \le a_{i, j} \le 2 \cdot 10^5$

题目描述

You are given a rectangular matrix of size $ n \times m $ consisting of integers from $ 1 $ to $ 2 \cdot 10^5 $ . In one move, you can: - choose any element of the matrix and change its value to any integer between $ 1 $ and $ n \cdot m $ , inclusive; - take any column and shift it one cell up cyclically (see the example of such cyclic shift below). A cyclic shift is an operation such that you choose some $ j $ ( $ 1 \le j \le m $ ) and set $ a_{1, j} := a_{2, j}, a_{2, j} := a_{3, j}, \dots, a_{n, j} := a_{1, j} $ simultaneously. ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF1294E/8565157bd76bb2dbe391e7f12ac2946eff0ed96a.png) Example of cyclic shift of the first column You want to perform the minimum number of moves to make this matrix look like this: ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF1294E/f0a34fa55590e79e2f0e0af3c39b0d9ee30b43c2.png)In other words, the goal is to obtain the matrix, where $ a_{1, 1} = 1, a_{1, 2} = 2, \dots, a_{1, m} = m, a_{2, 1} = m + 1, a_{2, 2} = m + 2, \dots, a_{n, m} = n \cdot m $ (i.e. $ a_{i, j} = (i - 1) \cdot m + j $ ) with the minimum number of moves performed.

输入输出格式

输入格式


The first line of the input contains two integers $ n $ and $ m $ ( $ 1 \le n, m \le 2 \cdot 10^5, n \cdot m \le 2 \cdot 10^5 $ ) — the size of the matrix. The next $ n $ lines contain $ m $ integers each. The number at the line $ i $ and position $ j $ is $ a_{i, j} $ ( $ 1 \le a_{i, j} \le 2 \cdot 10^5 $ ).

输出格式


Print one integer — the minimum number of moves required to obtain the matrix, where $ a_{1, 1} = 1, a_{1, 2} = 2, \dots, a_{1, m} = m, a_{2, 1} = m + 1, a_{2, 2} = m + 2, \dots, a_{n, m} = n \cdot m $ ( $ a_{i, j} = (i - 1)m + j $ ).

输入输出样例

输入样例 #1

3 3
3 2 1
1 2 3
4 5 6

输出样例 #1

6

输入样例 #2

4 3
1 2 3
4 5 6
7 8 9
10 11 12

输出样例 #2

0

输入样例 #3

3 4
1 6 3 4
5 10 7 8
9 2 11 12

输出样例 #3

2

说明

In the first example, you can set $ a_{1, 1} := 7, a_{1, 2} := 8 $ and $ a_{1, 3} := 9 $ then shift the first, the second and the third columns cyclically, so the answer is $ 6 $ . It can be shown that you cannot achieve a better answer. In the second example, the matrix is already good so the answer is $ 0 $ . In the third example, it is enough to shift the second column cyclically twice to obtain a good matrix, so the answer is $ 2 $ .