import sys
input = sys.stdin.readline
from collections import deque

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




distance = [[min(i, j, n - i - 1, n - j - 1) for i in range(n)] for j in range(n)]
check = [[1] * n for i in range(n)]
p = [((i-1)//n, (i-1) % n) for i in p]
ans=0
for i,j in p:
    ans+=distance[j][i]
    d = deque()
    d.append((j, i, distance[j][i]))
    check[j][i] = 0
    while len(d):
        x, y, dis = d.popleft()
        for i in range(0, 4):
            nx, ny = x + [1, 0, -1, 0][i], y + [0, 1, 0, -1][i]

            if 0 <= nx < n and 0 <= ny < n:
                # 更新できるマスは更新
                if distance[nx][ny] > dis:
                    distance[nx][ny] = dis
                    d.append((nx, ny, dis + check[nx][ny]))

print(ans)