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:
            temp[i-1]=cost
            if i-N>=0:
                temp[i-N]=cost
            if i+N<=N*N-1:
                temp[i+N]=cost
        elif (i%N)==0:
            temp[i+1]=cost
            if i-N>=0:
                temp[i-N]=cost
            if i+N<=N*N-1:
                temp[i+N]=cost
        elif i<=N-1:
            temp[i+N]=cost
            temp[i+1]=cost
            temp[i-1]=cost
        elif i>=N*N-N:
            temp[i-N]=cost
            temp[i+1]=cost
            temp[i-1]=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:
        if (p in goal):
            short=[0 for _ in range(N)]
            mini=0
        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
        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()







