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. [Image] Example of cyclic shift of the first column 

You want to perform the minimum number of moves to make this matrix look like this: $\left. \begin{array}{|c c c c|} \hline 1 & {2} & {\ldots} & {m} \\{m + 1} & {m + 2} & {\ldots} & {2m} \\{\vdots} & {\vdots} & {\ddots} & {\vdots} \\{(n - 1) m + 1} & {(n - 1) m + 2} & {\ldots} & {nm} \\ \hline \end{array} \right.$ 

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-----

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-----

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$).


-----Examples-----
Input
3 3
3 2 1
1 2 3
4 5 6

Output
6

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

Output
0

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

Output
2



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

Difficulty: introductory

Starter code:

Solutions:
import sys
input = sys.stdin.readline

n, m = list(map(int, input().split()))
arr = [[] for i in range(m)]

for _ in range(n):
    l = list(map(int, input().split()))
    for i in range(m):
        arr[i].append(l[i])

out = 0
for i in range(m):
    l = arr[i]
    
    best = list(range(0, -n, -1))
    for j in range(n):
        v = l[j] - i - 1
        
        if v % m == 0:
            correct = v // m
            if 0 <= correct < n:
                best[j - correct] += 1
    out += (n - max(best))

print(out)


import sys

def minp():
	return sys.stdin.readline().strip()

def mint():
	return int(minp())

def mints():
	return list(map(int, minp().split()))

def solve():
	n, m = mints()
	a = [None] * n
	for i in range(n):
		a[i] = list(mints())
	r = 0
	for j in range(m):
		c = [0]*n
		for i in range(n):
			x = (a[i][j]-1)
			#print(x, end='')
			if x % m == j and x < n*m:
				t = (x-j) // m
				#print('',t,i,end='')
				if i-t >= 0:
					c[i-t] += 1
				else:
					c[i-t+n] += 1
			#print(end='|')
		#print()
		mi = int(1e9)
		#print(c)
		for i in range(n):
			mi = min(mi, i+n-c[i])
		r += mi
	print(r)

solve()



import sys
input = sys.stdin.readline

n,m=list(map(int,input().split()))

CR=[list(map(int,input().split())) for i in range(n)]
for i in range(n):
    for j in range(m):
        CR[i][j]-=1
        
MAX=n*m
ANS=0

for i in range(m):
    SCORE=[0]*n

    for j in range(n):
        if CR[j][i]%m==i%m and 0<=CR[j][i]<MAX:
            x=(CR[j][i]-i%m)//m
            SCORE[(j-x)%n]+=1

        #print(SCORE)

    #print(SCORE)

    ANS+=min([n+j-SCORE[j] for j in range(n)])

print(ANS)


n, m = (int(i) for i in input().split())
a = [[int(i) for i in input().split()] for _ in range(n)]
res = 0
for j in range(m):
	b = [0] * n
	for i in range(n):
		if a[i][j] <= n*m and (a[i][j]-j-1)%m == 0:
			pos = (a[i][j]-j-1)//m
			shift = i-pos if i>=pos else i-pos+n
			b[shift] += 1
	res += min(i+n-b[i] for i in range(n))
print(res)


"""
NTC here
"""
import sys
inp= sys.stdin.readline
input = lambda : inp().strip()
flush= sys.stdout.flush
# import threading
# sys.setrecursionlimit(10**6)
# threading.stack_size(2**25)

def iin(): return int(input())
def lin(): return list(map(int, input().split()))

# range = xrange
# input = raw_input

def main():
    n, m = lin()
    a= [lin() for i in range(n)]
    a = [[a[j][i] for j in range(n)] for i in range(m)]
    ans = 0
    #print(*a, sep='\n')
    for i in range(m):
        dc = {}
        for j in range(n):
            x = a[i][j]-i-1
            if x>=0 and (x)%m==0 and x//m<n:
                #print(a[i][j], x, x//m, (j-x//m), (j-x//m)%n)
                try:
                    dc[(j-x//m)%n]+=1
                except:
                    dc[(j-x//m)%n]=1
        ch1, ch2 = 0, -1
        ch3 = n
        for i in dc:
            ch3 = min(ch3, i+n-dc[i])
        #print(dc, ch1)
        ans += min(n, ch3)
    print(ans)





            
        

        
main()
# threading.Thread(target=main).start()