n=int(input())
*p,=map(int, input().split())
#n=3
#p=[1, 3, 7, 9, 5, 4, 8, 6, 2]

for i in range(n*n):p[i]-=1
seats=[
    [min(i,j,n-i-1,n-j-1)for i in range(n)] 
    for j in range(n)
    ]

check=[[False]*n for _ in range(n)]
exist=[[1]*n for _ in range(n)]
dxy=[(0,1),(1,0),(-1,0),(0,-1)]
def dfs(pi):
    global check,exist
    i=pi%n
    j=pi//n
    res=seats[i][j]
    check[i][j]=True
    for dx,dy in dxy:
        idx,jdy=i+dx,j+dy
        if 0<=idx<n and 0<=jdy<n and (not check[idx][jdy]):
            res=min(res,dfs(idx+jdy*n)+exist[idx][jdy])
        if res==0:
            return res
    seats[i][j]=res
    return res
ans=0
for pi in p:
    check=[[False]*n for _ in range(n)]
    ans+=dfs(pi)
    exist[pi%n][pi//n]=0
print(ans)