import sys
from collections import deque
from heapq import heapify, heappop, heappush

def solve():
      input = sys.stdin.readline
      N = int(input())
      P = [int(p) - 1 for p in input().split()]
      fin = [[1 for j in range(N)] for i in range(N)]
      move = ((-1, 0), (0, -1), (1, 0), (0, 1))
  
      count = 0
      for i, p in enumerate(P):
            x, y = p // N, p % N
            q = []
            heapify(q)
            fin[x][y] = 0
            heappush(q, (0, x, y))
            minCount = 1000000
            path = dict()
            while q:
                  cost, nx, ny = heappop(q)
                  if (nx, ny) not in path:
                        path[(nx, ny)] = cost
                        if nx == 0 or nx == N-1 or ny == 0 or ny == N-1:
                              count += cost
                              break
                        else:
                              for dx, dy in move:
                                    if 0 <= nx+dx < N and 0 <= ny+dy<N:
                                          heappush(q, (cost + fin[nx+dx][ny+dy], nx+dx, ny+dy))

      print(count)
            
      return 0

if __name__ == "__main__":
      solve()