import heapq

INF = 10**3
n = int(input())
A = list(map(int,input().split()))

F = [[True]*n for _ in range(n)]
V = [[False]*n for _ in range(n)]

def escape(F,ys,xs,n):
  if xs==0 or xs==n-1 or ys==0 or ys==n-1 or V[ys][xs]:    
    return 0  
    
  G = [[INF]*n for _ in range(n)]
  G[ys][xs] = 0
  
  que = [(0,ys,xs)] 
  while len(que)>0:
    c,y,x = heapq.heappop(que)    
    if x==0 or x==n-1 or y==0 or y==n-1 or V[y][x]:      
      return c
    
    for dy,dx in ((0,1),(1,0),(0,-1),(-1,0)):
      nx = dx+x
      ny = dy+y
      
      if G[ny][nx] > G[y][x]:
        G[ny][nx] = G[y][x] + F[ny][nx]        
        heapq.heappush(que,(G[ny][nx],ny,nx))        
    
ans = 0
for a in A:
  y = (a-1) //n
  x = (a-1) % n
  F[y][x] = False
  
  if x==0 or x==n-1 or y==0 or y==n-1:
    V[y][x] = True
  else:
    for dy,dx in ((0,1),(1,0),(0,-1),(-1,0)):
      nx = dx+x
      ny = dy+y
      if V[ny][nx] == True:
        V[y][x] = True
                    
  c = escape(F,y,x,n)  
      
  ans += c
  
print(ans)  