import sys
input = sys.stdin.readline

N=int(input())
P=list(map(int,input().split()))

DP=[[-1]*N for i in range(N)]
for i in range(N):
    for j in range(N):
        DP[i][j]=min(i,j,N-1-i,N-1-j)

USED=[[0]*N for i in range(N)]

ANS=0
    
for p in P:
    q,r=divmod(p-1,N)
    ANS+=DP[q][r]
    DP[q][r]-=1
    USED[q][r]=1

    Q=[(q,r)]

    while Q:
        x,y=Q.pop()

        for z,w in [(x+1,y),(x-1,y),(x,y+1),(x,y-1)]:
            if 0<=z<N and 0<=w<N:
                if USED[z][w]==1 and DP[z][w]>DP[x][y]:
                    DP[z][w]=DP[x][y]
                    Q.append((z,w))
                    
                elif DP[z][w]>DP[x][y]+1:
                    DP[z][w]=DP[x][y]+1
                    Q.append((z,w))
    
    #for d in DP:
    #    print(*d)
    #print(ANS)
    #print()
print(ANS)
    
