def cur(i,j):
    rank=g[i][j]+sit[i][j]
    if i+1<n and g[i+1][j]>rank:
        g[i+1][j]=rank
        cur(i+1,j)
    if i-1>-1 and g[i-1][j]>rank:
        g[i-1][j]=rank
        cur(i-1,j)
    if j+1<n and g[i][j+1]>rank:
        g[i][j+1]=rank
        cur(i,j+1)
    if j-1>-1 and g[i][j-1]>rank:
        g[i][j-1]=rank
        cur(i,j-1)

n=int(input())
g=[[min(i,n-1-i,j,n-1-j) for j in range(n)] for i in range(n)]
sit=[[1 for j in range(n)] for i in range(n)]
p=list(map(int,input().split()))

ans=0
for tp in p:
    pi,pj=(tp-1)//n,(tp-1)%n
    sit[pi][pj]=0
    ans+=g[pi][pj]
    cur(pi,pj)
print(ans)