from collections import deque
import sys

input = sys.stdin.buffer.readline

N = int(input())
Order = list(map(int, input().split()))
Maze = [[1] * N for _ in range(N)]
dy = (1, 0, -1, 0)
dx = (0, 1, 0, -1)
ans = 0
for o in Order:
    o -= 1
    sx = o % N
    sy = o // N
    Q = deque()
    now = (0, sx, sy)
    Q.append(now)
    D = [[0] * N for _ in range(N)]
    D[sy][sx] = 1
    while len(Q) > 0:
        fatigue, x, y = Q.popleft()
        if x < 0 or y < 0 or x >= N or y >= N:
            ans += fatigue
            if fatigue == 0:
                D[sy][sx] = 2
            break
        if D[y][x] == 2:
            ans += fatigue
            if fatigue == 0:
                D[sy][sx] = 2
            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:
                Q.appendleft((fatigue, nx, ny))
            elif D[ny][nx] == 1:
                continue
            else:
                D[ny][nx] = 1
                if Maze[ny][nx] != 0:
                    Q.append((fatigue + 1, nx, ny))
                else:
                    Q.appendleft((fatigue, nx, ny))

    Maze[sy][sx] = 0

print(ans)