import math
from collections import deque
import numpy as np
import sys
sys.setrecursionlimit(1000000)

n = int(input())
p = map(lambda x: int(x) - 1, input().split())
cost = np.zeros((n, n))
q = deque(p)
dir = [(1, 0), (-1, 0), (0, 1), (0, -1)]
for i in range(math.ceil(n / 2)):
    cost[i:-i, i] = i
    cost[i:-i, -(i + 1)] = i
    cost[i, i:-i] = i
    cost[-(i + 1), i:-i] = i
ans = 0
used = np.zeros((n, n))


def attached(x, y, new_cost):
    for i, j in dir:
        if 0 <= x + i < n and 0 <= y + j < n and not(used[x + i, y + j]):
            if cost[x + i, y + j] > new_cost:
                cost[x + i, y + j] = new_cost
        elif 0 <= x + i < n and 0 <= y + j < n and used[x + i, y + j]:
            attached(x + 1, y + 1, new_cost)


while q:
    pe = q.popleft()
    x = pe // n
    y = pe % n
    ans += cost[x, y]
    used[x, y] = 1
    attached(x, y, cost[x, y])

    print(cost, pe, ans)
print(int(ans))
