# 他の方のコードで実験
from _collections import deque
N = int(input())
P = list(map(int,input().split()))
K = [( (i-1)//N, (i-1) % N ) for i in P]
used = [[1]*N for _ in range(N)]
C = [[min([i, N-i-1, j, N-j-1]) for j in range(N)] for i in range(N)]

ans = 0
move = [(1, 0), (-1, 0), (0, 1), (0, -1)]
for i in range(N*N):
    a,b = K[i]
    used[a][b] = 0
    ans += C[a][b]
    q = deque([(a, b)])
    while(len(q)>0):
        s,t = q.popleft()
        for j,k in move:
            after_s = s+j
            after_t = t+k
            if 0 <= after_s <= N-1:
                if 0 <= after_t <= N-1:
                    c = C[s][t]
                    if used[s][t] == 1:
                        c += 1
                    if c < C[after_s][after_t]:
                        C[after_s][after_t] = c
                        q.append((after_s, after_t))
print(ans)