import numpy as np
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(100000)

from numba import njit

@njit('i4,i4[:]',cache=True)
def main(n,p):
    dx = np.array([0,1,0,-1])
    dy = np.array([1,0,-1,0])
    ans = 0
    def dfs(x,y,dx,dy,grid,dis):
        z = dis[y][x] + grid[y][x]
        for i in range(4):
            next_x = x+dx[i]
            next_y = y+dy[i]
            if dis[next_y][next_x] > z:
                dis[next_y][next_x] = z
                dfs(next_x,next_y,dx,dy,grid,dis)
    grid = np.ones((n+1,n+1),np.int32)
    dis = np.zeros((n+2,n+2),np.int32)
    for i in range(1,n+1):
        for j in range(1,n+1):
            dis[i][j] = min(i,n-i+1,j,n-j+1)-1
    for i in range(n**2):
        pos = p[i]
        x = (pos-1)%n+1
        y = (pos-1)//n+1
        ans += dis[y][x]
        grid[y][x] = 0
        dfs(x,y,dx,dy,grid,dis)
    return ans

n = int(readline())
p = np.array(readline().split(),np.int32)
print(main(n,p))