import collections
import sys
input=sys.stdin.readline

def solve():
  n=int(input())
  arr=list(map(int,input().split()))
  cost=[[30000]*n for _ in range(n)]
  for i in range(n):
    for j in range(n):
      cost[i][j]=min(i,n-(i+1),j,n-(j+1))
  board=[[1]*n for _ in range(n)]
  ans=0
  for val in arr:
    x=(val-1)%n
    y=(val-1)//n
    ans+=cost[y][x]
    board[y][x]=0
    q=collections.deque()
    q.append((x,y))
    while len(q)!=0:
      x,y=q.popleft()
      c=cost[y][x]+board[y][x]
      if x>0 and c<cost[y][x-1]:
        cost[y][x-1]=c
        q.append((x-1,y))    
      if x<n-1 and c<cost[y][x+1]:
        cost[y][x+1]=c
        q.append((x+1,y))    
      if y>0 and c<cost[y-1][x]:
        cost[y-1][x]=c
        q.append((x,y-1))    
      if y<n-1 and c<cost[y+1][x]:
        cost[y+1][x]=c
        q.append((x,y+1))
  print(ans)
solve()