import heapq

N = int(input())
Order = list(map(int, input().split()))
Maze = [[0] * N for _ in range(N)]
for i in range(N):
    for j in range(N):
        Maze[i][j] = i * N + j + 1
cnt = 1
dy = (1, 0, -1, 0)
dx = (0, 1, 0, -1)
ans = 0
Done = [[-1] * N for _ in range(N)]
for o in Order:
    o -= 1
    sx = o % N
    sy = o // N
    Q = []
    now = (0, sx, sy)
    heapq.heappush(Q, now)
    D = [[False] * N for _ in range(N)]
    D[sy][sx] = True
    while len(Q) > 0:
        fatigue, x, y = heapq.heappop(Q)
        if x < 0 or y < 0 or x >= N or y >= N:
            ans += fatigue
            Done[sy][sx] = fatigue
            break
        for d in range(4):
            ny = y + dy[d]
            nx = x + dx[d]
            if nx < 0 or ny < 0 or nx >= N or ny >= N:
                heapq.heappush(Q, (fatigue, nx, ny))
            elif D[ny][nx]:
                continue
            else:
                D[ny][nx] = True
                if Done[ny][nx] != -1:
                    heapq.heappush(Q, (fatigue + Done[ny][nx], -1, -1))
                if Maze[ny][nx] != 0:
                    heapq.heappush(Q, (fatigue + 1, nx, ny))
                else:
                    heapq.heappush(Q, (fatigue, nx, ny))

    Maze[sy][sx] = 0

print(ans)