import sys
sys.setrecursionlimit(10**5)
N = int(input())
P = list(map(int, input().split()))

cost = [[N]*N for i in range(N)]

for i in range(N):
    cost[i][0] = 0
    cost[i][-1] = 0
    cost[0][i] = 0
    cost[-1][i] = 0

for i in range(1, N-1):
    for j in range(1, N-1):
        cost[i][j] = min(cost[i+1][j], cost[i-1][j],
                         cost[i][j+1], cost[i][j-1]) + 1

s = set()
def update(i, j):
    if i > 0:
        if cost[i][j]+1 == cost[i-1][j]:
            if (i-1, j) in s:
                cost[i-1][j] -= 1
                update(i-1, j)

            else:
                update(i-1, j)
                cost[i-1][j] -= 1
    if i < N-1:
        if cost[i][j]+1 == cost[i+1][j]:
            if (i+1, j) in s:
                cost[i+1][j] -= 1
                update(i+1, j)
            else:
                update(i+1, j)
                cost[i+1][j] -= 1
    if j > 0:
        if cost[i][j]+1 == cost[i][j-1]:
            if (i, j-1) in s:
                cost[i][j-1] -= 1
                update(i, j-1)
            else:
                update(i, j-1)
                cost[i][j-1] -= 1
    if j < N-1:
        if cost[i][j]+1 == cost[i][j+1]:
            if (i, j+1) in s:
                cost[i][j+1] -= 1
                update(i, j+1)
            else:
                update(i, j+1)
                cost[i][j+1] -= 1


ans = 0

for x in P:
    i = (x - 1) // N
    j = (x-1) % N
    ans += cost[i][j]
    # if cost[i][j]:
    #     print(x)
    #     print(*cost, sep='\n')
    #     print(ans)
    update(i, j)
    s.add((i, j))

print(ans)
