from queue import Queue

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 for i in range(N)] for j in range(N)]

tx = [1, 0, -1, 0]
ty = [0, 1, 0, -1]

ans = 0

for p in P:
    x = (p-1) // N
    y = (p-1) % N
    d[x][y] = 0
    cnt = float('INF')
    for i in range(4):
        nx = x + tx[i]
        ny = y + ty[i]
        if 0 <= nx < N and 0 <= ny < N:
            cnt = min(cnt, MAP[nx][ny] + d[nx][ny])
        else:
            cnt = min(cnt, 0)
    
    ans += MAP[x][y]
    
    MAP[x][y] = cnt
    
    q = Queue()
    
    q.put((x, y, cnt))
    
    while not q.empty():
        x, y, cnt = q.get()
        for i in range(4):
            nx = x + tx[i]
            ny = y + ty[i]
            if 0 <= nx < N and 0 <= ny < N and MAP[nx][ny] > MAP[x][y] + d[x][y]:
                MAP[nx][ny] = MAP[x][y] + d[x][y]
                q.put((nx, ny, cnt + d[x][y]))
    

print(ans)