N=int(input())
import heapq
def dijkstra(s,graph,n):
    dist=[float("inf") for i in range(n+1)]
    dist[s]=0
    pq=[]
    heapq.heapify(pq)
    heapq.heappush(pq,(0,s))
    while pq:
        x=heapq.heappop(pq)
        node,mini_dis=x[1],x[0]
        for point in graph[node].keys():
            w=graph[node][point]
            newlen=dist[node]+w
            if newlen<dist[point]:
                heapq.heappush(pq,(newlen,point))
                dist[point]=newlen
    return dist

que=list(map(int,input().split()))
from collections import defaultdict
dif=[1,-1,N,-N]
graph={i:defaultdict(lambda x:1) for i in range(N**2+1)}
for i in range(N):
    for j in range(N):
        node=N*i+j+1
        for num in dif:
            if 1<=node+num<=N**2:
                graph[node][node+num]=1
                graph[node+num][node]=1
ans=0
for i in range(N):
    graph[0][i+1]=0
    graph[i+1][0]=0
    graph[0][N*(N-1)+i+1]=0
    graph[N*(N-1)+i+1][0]=0
vanished=[False for i in range(N**2+1)]
for i in range(N):
    graph[0][i*N+1]=0
    graph[i*N+1][0]=0
    graph[0][i*N+N]=0
    graph[i*N+N][0]=0
    
for some in que:
    ans+=dijkstra(some,graph,N**2)[0]
    vanished[some]=True
 ##   print(dijkstra(0,graph,N**2)[some])
    for num in dif:
        if 1<=num+some<=N**2:
            if vanished[num+some]:
                graph[some][num+some]=0
                graph[num+some][some]=0
            else:
                graph[num+some][some]=0
print(ans)    