def main(N,P):
    A=[-1]*4*N*N
    ad=(1,N,-1,-N)
    for I in range(4*N*N):
        i,j=I//4,I%4
        if 0<=i+ad[j]<N*N:
            A[i*4+j]=i+ad[j]
    X=[0]*N*N
    for i in range(N*N):
        X[i]=min(i//N,N-1-i//N,i%N,N-1-i%N)
    E=[1]*N*N
    a=0
    q=[0]*N*N*N
    l,r=0,0
    for p in P:
        p-=1
        a+=X[p]
        E[p]=0
        q[r]=p
        r+=1
        while l<r:
            i=q[l]
            l+=1
            for j in A[4*i:4*(i+1)]:
                if 0<=j and X[i]+E[i]<X[j]:
                    X[j]=X[i]+E[i]
                    q[r]=j
                    r+=1
    return a
import sys
import numpy as np
readline=sys.stdin.buffer.readline
read=sys.stdin.buffer.read
if sys.argv[-1]=='ONLINE_JUDGE':
    from numba import*
    from numba.pycc import CC
    cc=CC('my_module')
    def cc_export(f,signature):
        cc.export(f.__name__,signature)(f)
        return njit(f)
    main=cc_export(main,(i8,i8[:],))
    cc.compile()
    exit()
N=int(readline())
P=np.array(readline().split(),dtype=np.int64)
from my_module import main
print(main(N,P))
