from collections import deque
n=int(input())
p= list(map(int, input().split()))

# 迷路　BFS
def bfs(sx,sy):
    d = deque()
    d.append((sx, sy,distance[sx][sy]))
    ans=distance[sx][sy]
    v=distance[sx][sy]
    check[sx][sy]=0

    while len(d):
        x, y,dis = d.popleft()
        for i in range(0, 4):
            nx, ny = x + [1, 0, -1, 0][i], y + [0, 1, 0, -1][i]

            if 0 <= nx < n and 0 <= ny < n:
                # 更新できるマスは更新
                if distance[nx][ny]>dis:
                    distance[nx][ny]=dis
                    d.append((nx, ny,dis+check[nx][ny]))

    return ans

distance = [[0 for i in range(n)] for j in range(n)]
for i in range(n):
    for j in range(n):
        distance[i][j]+=min(i,j,n-i-1,n-j-1)
check = [[1]*n for i in range(n)]


anss=0
for i in p:
    x=(i-1)//n
    y=(i-1)%n
    anss+=bfs(x,y)

print(anss)