import numpy as np

def dfs(x,y):
    z = h[y][x] + grid[y][x]
    for i in range(4):
        next_x = x+dx[i]
        next_y = y+dy[i]
        if h[next_y][next_x] > z:
            h[next_y][next_x] = z
            dfs(next_x,next_y)
n = int(input())
h = np.array([[0]*(n+2) for _ in range(n+2)])
for i in range(1,n+1):
    for j in range(1,n+1):
        h[i][j] = min(i,n-i+1,j,n-j+1)-1
grid = np.array([[1]*(n+1) for _ in range(n+1)])
dx = np.array([0,1,0,-1])
dy = np.array([1,0,-1,0])
ans = 0
p = list(map(int,input().split()))
for i in range(n**2):
    pos = p[i]
    x = (pos-1)%n+1
    y = (pos-1)//n+1
    ans += h[y][x]
    grid[y][x] = 0
    dfs(x,y)
print(ans)
