N=int(input())
plist=list(map(int,input().split()))

sit=[[1]*N for _ in range(N)]
dist=[[min(i,j,N-1-i,N-1-j) for j in range(N)] for i in range(N)]

answer=0
for p in plist:
  i=(p-1)//N
  j=(p-1)%N
  #print(i,j)
  sit[i][j]=0
  answer+=dist[i][j]
  
  queue=[(i,j,dist[i][j])]
  d=0
  while queue:
    x,y,d=queue.pop()
    if x>0 and dist[x-1][y]>d:
      dist[x-1][y]=d
      queue.append((x-1,y,d+sit[x-1][y]))
    if x<N-1 and dist[x+1][y]>d:
      dist[x+1][y]=d
      queue.append((x+1,y,d+sit[x+1][y]))    
    if y>0 and dist[x][y-1]>d:
      dist[x][y-1]=d
      queue.append((x,y-1,d+sit[x][y-1]))
    if y<N-1 and dist[x][y+1]>d:
      dist[x][y+1]=d
      queue.append((x,y+1,d+sit[x][y+1]))
      
print(answer)