import sys import io, os input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline from itertools import accumulate from collections import defaultdict INF = 10**18 n = int(input()) A = [list(map(int, input().split())) for i in range(n)] D = defaultdict(lambda: []) for x in range(n): for y in range(n): a = A[x][y] D[a-1].append((x, y)) ans = 0 for a in range(n): imos = [0]*(n+1) imos1 = [0]*(n+1) imos2 = [0]*(n+1) for x, y in D[a]: imos1[0] += 1 imos1[max(0, x-y)] -= 1 imos2[min(x+y+1, n)] += 1 imos2[n] -= 1 imos1 = list(accumulate(imos1)) imos2 = list(accumulate(imos2)) for i in range(n, 0, -1): imos1[i-1] += imos1[i] for i in range(n): imos2[i+1] += imos2[i] for i in range(n): imos[i] = imos1[i]+imos2[i] pos = -1 M = INF for i in range(n): if M > imos[i]: M = imos[i] pos = i for x, y in D[a]: ans += max(abs(x-pos), y) print(ans)