from numba import njit
import numpy as np
@njit
def func(x,h):
    # hを更新
    c=h[x]
    h[x]=-1
    todo=[[x,c]]
    seen={x}
    while todo:
        v,c=todo.pop()
        x,y=divmod(v,n)
        for dx,dy in ((0,1),(0,-1),(1,0),(-1,0)):
            nx,ny=x+dx,y+dy
            if 0<=nx<n and 0<=ny<n:
                nv=nx*n+ny
                if nv in seen:continue
                if h[nv]==-1:
                    todo.append([nv,c])
                elif h[nv]>c:
                    h[nv]=c
                    todo.append([nv,c+1])
                seen.add(nv)
@njit
def main(n,p):
    h=np.array([min(i//n,n-i//n-1,i%n,n-i%n-1) for i in range(n**2)],np.int32)
    ans=0
    for x in p:
        ans+=h[x]
        func(x,h)
    print(ans)
if __name__=='__main__':
    n=int(input())
    p=np.array(list(map(int,input().split())),np.int32)
    p-=1
    main(n,p)
