from collections import deque

N = int(input())
P = list(map(int,input().split()))

MAP = [min(i, j, N-i-1, N-j-1) for i in range(N) for j in range(N)]

d = [1] * (N ** 2)

ans = 0

def update(np, p):
    if MAP[np] > MAP[p] + d[p]:
        q.append(np)
        MAP[np] = MAP[p] + d[p]

for p in P:
    p -= 1
    
    d[p] = 0
    
    ans += MAP[p]
    
    q = deque()
    
    q.append(p)
    
    while len(q) != 0:
        p = q.popleft()
        if p % N != N-1:
            update(p+1, p)
        if p % N != 0:
            update(p-1, p)
        if p // N != N-1:
            update(p+N, p)
        if p // N != 0:
            update(p-N, p)

print(ans)