N = int(input())
P = list(map(int,input().split()))
import heapq
def dijkstra_heap(s):
    #始点sから各頂点への最短距離
    d = [float("inf")] * (N**2)
    used = [True] * (N**2) #True:未確定
    d[s] = 0
    used[s] = False
    edgelist = []
    for e in edge[s]:
        heapq.heappush(edgelist,e)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        #まだ使われてない頂点の中から最小の距離のものを探す
        if not used[minedge[1]]:
            continue
        v = minedge[1]
        d[v] = minedge[0]
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,[e[0]+d[v],e[1]])
    return d
edge = [[] for i in range(N**2)]
for i in range(N):
    for j in range(N):
        x = i*N + j
        if (i == 0) or (i == N - 1) or (j == 0) or (j == N-1):
            edge[x].append([0,0])
            edge[0].append([0,x])
            continue
        if j != 0:
            edge[x].append([1,x-1])
            edge[x-1].append([1,x])
        if j != N-1:
            edge[x].append([1,x+1])
            edge[x+1].append([1,x])
        if i != 0:
            edge[x].append([1,x-N])
            edge[x-N].append([1,x])
        if i != N -1:
            edge[x].append([1,x+N])
            edge[x+N].append([1,x])
ans = 0
for p in P:
    x = p-1
    a = x // N
    b = x % N 
    ans += dijkstra_heap(0)[x]
    if b != 0:
        edge[x].append([0,x-1])
        edge[x-1].append([0,x])
    if b != N-1:
        edge[x].append([0,x+1])
        edge[x+1].append([0,x])
    if a != 0:
        edge[x].append([0,x-N])
        edge[x-N].append([0,x])
    if a != N-1:
        edge[x].append([0,x+N])
        edge[x+N].append([0,x])
print(ans)