from collections import deque

n = int(input())
p = list(map(int, input().split()))

rev_p = [0] * (n * n)
for offset in range(len(p)):
    rev_p[p[offset] - 1] = offset

costs = [0] * (n * n)

for i in range(n*n):
    costs[i] = min(i // n, n - i // n - 1, i % n, n - i % n - 1)

removed = set()

ans = 0
for i, pi in enumerate(p):
    ans += costs[pi-1]
    removed.add(pi-1)

    d = deque()
    d.append((pi-1, costs[pi-1]))

    while d:
        pos, cost = d.popleft()

        for offset in [-1, -n, 1, n]:
            if 0 <= pos + offset < n * n:
                next_cost = cost if pos in removed else cost + 1

                if costs[pos+offset] > next_cost:
                    costs[pos+offset] = next_cost
                    d.append((pos+offset, next_cost))

print(ans)
