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


def bfs(init_v):
    next_v = deque([init_v])
    dist = [INF]*(n**2)
    dist[to_v(*init_v)] = 0
    MAP[to_v(*init_v)] = '.'
    mincost = INF
    quit = 0
    while next_v:
        if quit:
            break
        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[to_v(i, j)] < mincost:
                    mincost = dist[to_v(i, j)]
                    if mincost == 0:
                        quit = 1
                        break
                continue
            cost = 0 if MAP[to_v(x, y)] == '.'else 1
            if dist[to_v(x, y)] <= dist[to_v(i, j)]+cost:
                continue
            dist[to_v(x, y)] = dist[to_v(i, j)]+cost
            next_v.append((x, y))
    return dist, mincost


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


def to_v(i, j):
    return i*n+j


ans = 0
# よく考えたら、これO(N**4)だった
for p in P:
    dist, mincost = bfs(to_idx(p))
    ans += mincost
print(ans)
