Obtain a Permutation
Contest: Codeforces Round 615 (Div. 3)
Difficulty rating: 1900
Tags: greedy, implementation, math

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.

Example of cyclic shift of the first column

You want to perform the minimum number of moves to make this matrix look like this:

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.

Input format:
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$$$).

Output format:
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$$$).

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
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$$$.

Example input:
3 3
3 2 1
1 2 3
4 5 6

Example output:
6

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

Example output:
0

Editorial:
At first, let's decrease all elements by one and solve the problem in $$$0$$$-indexation.
The first observation is that we can solve the problem independently for each column.
Consider the column $$$j$$$ $$$(j \in [0; m-1])$$$. It consists of elements $$$[j; m + j, 2m + j, \dots, (n-1)m + j]$$$. Now consider some element $$$a_{i, j}$$$ $$$(i \in [0; n - 1])$$$. We don't need to replace it with some other number in only one case: if we shift the column such that $$$a_{i, j}$$$ will coincide with the corresponding number of the required matrix. Obviously, there is only one cyclic shift of the column that can rid us of replacing $$$a_{i, j}$$$. So, the idea is the following: let's calculate for each cyclic shift the number of elements we don't need to replace if we use this cyclic shift. Let for the $$$i$$$-th cyclic shift ($$$0$$$-indexed) it be $$$cnt_i$$$. Then the answer for this column can be taken as $$$\min\limits_{i=0}^{n-1} n - cnt_i + i$$$.
How to calculate for the element $$$a_{i, j}$$$ the corresponding cyclic shift? Firstly, if $$$a_{i, j} \% m \ne j$$$ ($$$\%$$$ is modulo operation) then there is no such cyclic shift. Otherwise, let $$$pos = \lfloor\frac{a_{i, j}}{m}\rfloor$$$. If $$$pos < n$$$ then there is such cyclic shift ($$$pos$$$ can be greater than or equal to $$$n$$$ because $$$a_{i, j}$$$ can be up to $$$2 \cdot 10^5$$$) and the number of such cyclic shift is $$$(i - pos + n) \% n$$$. So let's increase $$$cnt_{(i - pos + n) \% n}$$$ and continue. After considering all elements of this column take the answer by the formula above and go to the next column.
Time complexity: $$$O(nm)$$$.