from collections import deque
n = int(input())
l = list(map(int,input().split()))
check = [[1]*n for i in range(n)]
d = [[float("INF")]*n for i in range(n)]

dx = [1,-1,0,0]
dy = [0,0,1,-1]
d[0] = [0]*n
d[-1] = [0]*n
for i in range(n):
    d[i][0] = 0
    d[i][-1] = 0
for i in range(1,n-1):
    for j in range(1,n-1):
        d[i][j] = min(d[i+1][j],d[i-1][j],d[i][j+1],d[i][j-1])+1

def update(x,y):
    check[x][y] = 0
    a = check[x][y]
    for i in range(4):
        nx = x+dx[i]
        ny = y+dy[i]
    
        if 0 <= nx < n and 0 <= ny < n:
            a = max(a,d[nx][ny])
    check[x][y] = min(check[x][y],a)
    q = deque([])
    q.append((x,y))
    while q:
        bx,by = q.popleft()

        for i in range(4):
            nx = bx+dx[i]
            ny = by+dy[i]

            if 0 <= nx < n and 0 <= ny < n:
                if d[nx][ny] > d[bx][by]+check[bx][by]:
                    d[nx][ny] = d[bx][by]
                    q.append((nx,ny))
                

ans = 0
for i in l:
    i -= 1
    x = i//n
    y = i%n
    ans += d[x][y]
    update(x,y)
print(ans)