from collections import deque
n=int(input())
*p,=map(int, input().split())

for i in range(n*n):p[i]-=1
seats=[
    [min(i,j,n-i-1,n-j-1) for i in range(n)] 
    for j in range(n)
    ]

exist=[[1]*n for _ in range(n)]
dxy=[(0,1),(1,0),(-1,0),(0,-1)]

ans=0
q=deque()
for pi in p:
    ans+=seats[pi//n][pi%n]
    exist[pi//n][pi%n]=0
    q.append((pi//n,pi%n))
    while q:
        i,j=q.popleft()
        for dx,dy in dxy:
            idx=i+dx;jdy=j+dy
            if 0<=idx<n and 0<=jdy<n:
                if seats[i][j]+exist[i][j]<seats[idx][jdy]:
                    seats[idx][jdy]=seats[i][j]+exist[i][j]
                    q.append((idx,jdy))


print(ans)