import numpy as np
from collections import deque
from scipy.sparse.csgraph import shortest_path

def main():
    N=int(input())
    P=list(map(lambda x:int(x)-1,input().split()))

    grid=np.zeros((N,N))

    graph=[]

    cost=N*N

    for i in range(N*N):
        temp=[0 for _ in range(N*N)]
        if (i+1)%N==0: #右端
            if i!=N-1 and i!=N**2-1:
                temp[i-1]=cost
        elif (i%N)==0: #左端
            if i!=0 and i!=N**2-N:
                temp[i+1]=cost
        elif i<=N-1: #下端
            if i!=0 and i!=N-1:
                temp[i+N]=cost
        elif i>=N*N-N: #上端
            if i!=N**2-N and i!=N**2-1:
                temp[i-N]=cost
        else:
            temp[i+1]=cost
            temp[i-1]=cost
            temp[i+N]=cost
            temp[i-N]=cost
        graph.append(temp)

    graph=np.array(graph)

    res=0
    goal=[]
    for i in range(N):
        goal.append(i)
        if i==0 or i==N-1:
            for j in range(N):
                goal.append(i+j*N)
        else:
            goal.append(i + N * (N - 1))
    for p in P:
        in_flag=0
        if (p in goal):
            short=[0 for _ in range(N)]
            mini=0
            in_flag=1
        else:
            short=shortest_path(graph,indices=p,method="D")
            mini=2*10**5
            for g in goal:
                mini=min(mini,short[g])
        res+=mini//cost
        
        if in_flag==0:
            edge=graph[p,:]
            link=[]
            for i in range(N):
                if edge[i]!=0:
                    link.append(i)
            if len(link)==4:
                link.sort()
                graph[link[0],link[3]]=edge[link[0]]
                graph[link[3], link[0]] = edge[link[0]]
                graph[link[1],link[2]]=edge[link[1]]
                graph[link[2],link[1]]=edge[link[1]]
                graph[:,p]=0
                graph[p,:]=0

        
        g_col=graph[:,p]
        g_col=np.where(g_col==cost,1,g_col)
        graph[:,p]=g_col
        


    print(int(res))

if __name__=="__main__":
    main()







