n = int(input())
p = list(map(int, input().split()))
s = [[0]*n for _ in range(n)]

ans = 0
def edge(N, i):
    if i % N == 0 or i % N == 1:
        return 1
    elif i > 1 and i <N:
        return 1
    elif i > N**2-N+1 and i<N**2:
        return 1
    else:
        return 0
    
def check(n, s, r, c):
    b =  1
    if not r ==0: #縦↑
        for j in range(r):
            if s[j][c]==0:
                b = 0
        if b==1:return 1
        else:b=1
        
    if not r == n-1: #縦↓
        for j in range(r+1, n):
            if s[j][c]==0:
                b = 0
        if b==1:return 1
        else:b=1
        
    if not c == 0: #横←
        for j in range(c):
            if s[r][j]==0:
                b = 0
        if b==1:return 1
        else:b=1
        
    if not c == n-1: #縦↓
        for j in range(c+1, n):
            if s[r][j]==0:
                b = 0
        if b==1:return 1  
    return 0

from collections import deque
def mini(n, s, r, c):
    seen = [[0]*n for _ in range(n)]
    x = [1, 0, -1, 0]
    y = [0, 1, 0, -1]
    queue = deque([r, c])
    t = 10**100
    while queue:
        qr = queue.popleft()
        qc = queue.popleft()
        temp = min(qr, qc, n-qr-1, n-qc-1)
        t = min(t, temp)

        for i in range(4):
            if qr+x[i]>=0 and qr+x[i]<n and  qc+y[i]>=0 and qc+y[i]<n:
                if s[qr+x[i]][qc+y[i]] == 1 and seen[qr+x[i]][qc+y[i]]==0:
                    queue.append(qr+x[i])
                    queue.append(qc+y[i])
                    seen[qr+x[i]][qc+y[i]]=1
    return t

for i in p:
    if i % n ==0:
        r = i//n-1
        c = n-1
    else:
        r = i//n
        c = i%n-1
    if edge(n, i):
        s[r][c] = 1
        continue
    elif check(n, s, r, c):
        s[r][c] = 1
        continue
    else:
        s[r][c] = 1
        ans += mini(n, s, r, c)    
        
print(ans)