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

mat=[[True]*N for _ in range(N)]
answer=0
for p in plist:
  i=(p-1)//N
  j=(p-1)%N
  #print(i,j)
  
  queue=[(i,j)]
  visited=[[False]*N for _ in range(N)]
  reach=False
  d=0
  while queue:
    new_queue=set()
    
    while queue:
      x,y=queue.pop()
      if x==0 or x==N-1 or y==0 or y==N-1:
        reach=True
        break
        
      visited[x][y]=True
      if not visited[x-1][y]:
        if mat[x-1][y]:
          new_queue.add((x-1,y))
        else:
          queue.append((x-1,y))
      if not visited[x+1][y]:
        if mat[x+1][y]:
          new_queue.add((x+1,y))
        else:
          queue.append((x+1,y))
      if not visited[x][y-1]:
        if mat[x][y-1]:
          new_queue.add((x,y-1))
        else:
          queue.append((x,y-1))
      if not visited[x][y+1]:
        if mat[x][y+1]:
          new_queue.add((x,y+1))
        else:
          queue.append((x,y+1))
          
    if reach:
      break
      
    queue=list(new_queue)
    d+=1
    
  #print(i,j,d)
  answer+=d
  mat[i][j]=False
  
print(answer)