import sys
from collections import deque

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)]
      d = [[10000 for j in range(N)] for i in range(N)]      
      move = ((-1, 0), (0, -1), (1, 0), (0, 1))
      for i in range(N):
            for j in range(N):
                  ud = min(i, N-i-1)
                  lr = min(j, N-j-1)
                  d[i][j] = min(ud, lr)
  
      count = 0
      for i, p in enumerate(P):
            x, y = p // N, p % N
            count += d[x][y]
            fin[x][y] = 0
            q = deque()
            for dx, dy in move:
                  if 0<=x+dx<N and 0<=y+dy<N:
                        q.append((x+dx, y+dy, d[x][y] + fin[x][y]))
            while q:
                  nx, ny, cost = q.popleft()
                  if d[nx][ny] > cost:
                        d[nx][ny] = cost
                        for dx, dy in move:
                              if 0<=nx+dx<N and 0<=ny+dy<N:
                                    q.append((nx+dx, ny+dy, d[nx][ny] + fin[nx][ny]))

      print(count)
            
      return 0

if __name__ == "__main__":
      solve()