from collections import deque

N=int(input())
edge={i:[] for i in range(0,N**2+1)}

for i in range(1,N**2+1):
    if i%N!=1:
        edge[i].append(i-1)
    else:
        edge[i].append(0)
        edge[0].append(i)
    if i%N!=0:
        edge[i].append(i+1)
    else:
        edge[i].append(0)
        edge[0].append(i)
    if i>N:
        edge[i].append(i-N)
    else:
        edge[i].append(0)
        edge[0].append(i)
    if N**2-N+1>i:
        edge[i].append(i+N)
    else:
        edge[i].append(0)
        edge[0].append(i)

edge[0]=list(set(edge[0]))
P=list(map(int,input().split()))
dist={}
que=deque([(0,0)])
while que:
    v,d=que.popleft()
    if v not in dist:
        dist[v]=0
        dist[v]=d
    else:
        continue
    for nv in edge[v]:
        if nv not in dist:
            if v==0:
                que.appendleft((nv,d))
            else:
                que.append((nv,d+1))

remove=set([0])
ans=0
for i in range(N**2):
    V=P[i]
    ans+=dist[V]
    que=deque([(V,dist[V])])
    remove.add(V)
    while que:
        v,d=que.popleft()
        dist[v]=0
        dist[v]=d
        for nv in edge[v]:
            if v in remove:
                test=d
                if dist[nv]>test:
                    que.appendleft((nv,test))
            else:
                test=d+1
                if dist[nv]>test:
                    que.append((nv,test))

print(ans)
