from numba import njit
import numpy as np
@njit
def dfs(v,c,h,stay,todo):
    # hを更新
    todo[0]=v
    now=0
    while now>=0:
        vv=todo[now]
        now-=1
        x,y=divmod(vv,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 h[nv]>h[vv]+stay[vv]:
                    h[nv]=h[vv]
                    now+=1
                    todo[now]=nv

@njit
def main(n,p):
    h=[min(i//n,n-i//n-1,i%n,n-i%n-1) for i in range(n**2)]
    h=np.array(h,np.int32)
    stay=np.full(n*n,1,np.int32)
    todo=np.zeros(n*n,np.int32)
    ans=0
    for v in p:
        c=h[v]
        ans+=c
        stay[v]=0
        dfs(v,c,h,stay,todo)
    print(ans)

if __name__=='__main__':
    n=int(input())
    p=np.array(list(map(int,input().split())),np.int32)
    p-=1
    main(n,p)