from collections import deque

n = int(input())
seat = [[True]*n for i in range(n)]
lst = list(map(int, input().split()))

def place(m):
  m -= 1
  return [m//n, m%n]

ans = 0

for i in range(n**2):
  
  h = place(lst[i])
  seat[h[0]][h[1]] = False
  score = float('inf')
  
  if h[0] == 0 or h[0] == n-1 or h[1] == 0 or h[1] == n-1:
    continue
    
  sy, sx = h[0], h[1]
  dist = [[-1]*n for _ in range(n)]
  queue = deque([])
  dx = [1, 0, -1, 0]
  dy = [0, 1, 0, -1]

  dist[sy][sx] = 0
  queue.appendleft([sy, sx])

  while len(queue) > 0:
    v = queue.popleft() 

    for i in range(4):
    
      next_y, next_x = v[0]+dy[i], v[1]+dx[i]
      if not(0 <= next_y < n) or not(0 <= next_x < n):
        continue 
      if dist[v[0]][v[1]] + seat[next_y][next_x] >= dist[next_y][next_x] >= 0:
        continue
      
      dist[next_y][next_x] = dist[v[0]][v[1]] + seat[next_y][next_x]
    
      if seat[next_y][next_x]:
        queue.append([next_y, next_x])
      else:
        queue.appendleft([next_y, next_x])
    
      if next_y == 0 or next_y == n-1 or next_x == 0 or next_x == n-1:
        score = min(score, dist[next_y][next_x])
      
  ans += score
print(ans)