Problem: You are given a rectangular matrix of size consisting of integers from to .
In one move, you can:
- choose any element of the matrix and change its value to any integer between and , 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 () and set 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 (i.e. ) with the minimum number of moves performed.
Input Format: The first line of the input contains two integers and () — the size of the matrix.
The next lines contain integers each. The number at the line and position is ().
Output Format: Print one integer — the minimum number of moves required to obtain the matrix, where ().
Note: In the first example, you can set and then shift the first, the second and the third columns cyclically, so the answer is . It can be shown that you cannot achieve a better answer.
In the second example, the matrix is already good so the answer is .
In the third example, it is enough to shift the second column cyclically twice to obtain a good matrix, so the answer is .