import sys
input = sys.stdin.readline
from collections import deque

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

def dim1(x,y):
    return x*N+y

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

USED=[0]*(N*N)
ANS=0

def dir4(x):
    if x-N>=0:
        yield x-N
    if x+N<N*N:
        yield x+N
    if x%N!=0:
        yield x-1
    if x%N!=N-1:
        yield x+1

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

    Q=deque([p])

    while Q:
        x=Q.popleft()

        for z in dir4(x):
            if USED[z]==1 and DP[z]>DP[x]:
                DP[z]=DP[x]
                Q.append(z)
                    
            elif DP[z]>DP[x]+1:
                DP[z]=DP[x]+1
                Q.append(z)
    
    #for d in DP:
    #    print(*d)
    #print(ANS)
    #print()
print(ANS)