from heapq import heappop, heappush
n = int(input())
P = list(map(int, input().split()))
inf = 10 ** 9
away = [[False] * n for _ in range(n)]

def dijkstra(sx, sy, d):
    que = []
    d[sx][sy] = 0
    heappush(que, (0, sx, sy))

    while len(que):
        dist, cx, cy = heappop(que)

        if d[cx][cy] < dist:
            continue
        for dx, dy in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
            tx, ty = cx + dx, cy + dy
            if 0 <= tx < n and 0 <= ty < n:
                if away[tx][ty]:
                    cost = 0
                else:
                    cost = 1
                if d[tx][ty] > dist + cost:
                    d[tx][ty] = dist + cost
                    heappush(que, (d[tx][ty], tx, ty))
            else:
                return d[cx][cy]

ans = 0
for p in P:
    row, line = (p-1) // n, (p-1) % n
    d = [[inf] * n for _ in range(n)]
    ans += dijkstra(row, line, d)
    away[row][line] = True

print(ans)
