import sys, math
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
mod = 10**9 + 7

from pprint import pprint

N = ri()
P = rl()

dp = [[0]*N for _ in range(N)]

def init(xflip, yflip):
    for i in range(1, math.ceil(N/2)):
        for j in range(1, math.ceil(N/2)):
            ni, nj = i, j
            di, dj = -1, -1
            if xflip:
                nj = N-1-j
                dj = 1
            if yflip:
                ni = N-1-i
                di = 1
            dp[ni][nj] = min(dp[ni+di][nj],dp[ni][nj+dj])+1

for xflip, yflip in [(0,0),(1,0),(0,1),(1,1)]:
    init(xflip, yflip)

def get_ij(p):
    n = p-1
    i = n // N
    j = n % N
    return i, j

import collections
outs = {}
def update(i, j):
    outs[(i,j)] = 1
    queue = collections.deque()
    queue.append((i,j))
    while queue:
        i, j = queue.popleft()
        for di, dj in [(1,0),(-1,0),(0,1),(0,-1)]:
            ni, nj = i+di, j+dj
            if not (0 <= ni < N and 0 <= nj < N):
                continue
            if dp[ni][nj] > dp[i][j]:
                dp[ni][nj] = dp[i][j]
                if (ni,nj) in outs:
                    queue.append((ni,nj))

ans = 0
for p in P:
    n = p-1
    i, j = get_ij(p)
    ans += dp[i][j]
    update(i,j)
    #pprint(dp)
print(ans)
