from _collections import deque
N = int(input())
P = list(map(int,input().split()))
K = []
used = [[1]*N for _ in range(N)]

for i in range(N*N):
    K.append([(P[i]-1)//N, (P[i]-1) % N])

C = {}
for i in range(N):
    for j in range(N):
        C[(i,j)] = min([i, N-i-1, j, N-j-1])

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)