N=int(input())
P=list(map(int,input().split()))
A=[[x if 0<=x<N*N else -1 for x in (i+1,i+N,i-1,i-N)] for i in range(N*N)]
X=[min(i//N,N-1-i//N,i%N,N-1-i%N) for i in range(N*N)]
E=[1]*N*N
r=0
from collections import deque
q=deque()
for p in P:
    p-=1
    r+=X[p]
    E[p]=0
    q.append(p)
    while q:
        i=q.popleft()
        for j in A[i]:
            if 0<=j and X[i]+E[i]<X[j]:
                X[j]=X[i]+E[i]
                q.append(j)
print(r)