from collections import deque
n = int(input())
P = list(map(int, input().split()))
MAP = [['#']*n for _ in range(n)]
INF = 10**18
DIRC = [(-1, 0), (0, 1), (1, 0), (0, -1)]


def bfs(init_v):
    next_v = deque([init_v])
    dist = [[INF]*n for _ in range(n)]
    i, j = init_v
    dist[i][j] = 0
    MAP[i][j] = '.'
    mincost = INF
    while next_v:
        i, j = next_v.popleft()
        for di, dj in DIRC:
            x, y = i+di, j+dj
            if x < 0 or y < 0 or n <= x or n <= y:
                if dist[i][j] < mincost:
                    mincost = dist[i][j]
                continue
            cost = 0 if MAP[x][y] == '.'else 1
            if dist[x][y] <= dist[i][j]+cost:
                continue
            dist[x][y] = dist[i][j]+cost
            next_v.append((x, y))
    return dist, mincost


def to_idx(p):
    i, j = divmod(p-1, n)
    return i, j


ans = 0
for p in P:
    dist, mincost = bfs(to_idx(p))
    ans += mincost
print(ans)
