import sys
input = sys.stdin.readline
N = int(input())
P = list(map(lambda x:int(x)-1,input().split()))

costs = [[0]*N for _ in range(N)]
for i in range(N):
    for j in range(N):
        costs[i][j] = min(i,j,N-1-i,N-1-j)
exist = [[1]*N for _ in range(N)]

from collections import deque
dxy = [(0,1),(1,0),(0,-1),(-1,0)]

ans = 0
for p in P:
    y,x = divmod(p,N)
    ans += costs[y][x]
    exist[y][x] = 0
    q = deque([(x,y)])
    while q:
        x,y = q.popleft()
        for dx,dy in dxy:
            nx,ny = x+dx,y+dy
            if not 0 <= nx < N: continue
            if not 0 <= ny < N: continue
            if costs[ny][nx] > costs[y][x] + exist[y][x]:
                costs[ny][nx] = costs[y][x] + exist[y][x]
                q.append((nx,ny))
print(ans)