# include <iostream>
# include <cstdio>

using namespace std ;

int dis[505][505] ;
bool already[505][505] ;
int xx[4] = { 0 , 0 , 1 , -1 } ;
int yy[4] = { 1 , -1 , 0 , 0 } ;
 
void dfs( int x , int y )
{
    int nowdis = dis[x][y] + already[x][y] ;
    for ( int i = 0 ; i < 4 ; i++ )
    {
        int newx = x + xx[i] , newy = y + yy[i] ;
        if ( dis[newx][newy] > nowdis )
        {
            dis[newx][newy] = nowdis ;
            dfs( newx , newy ) ;
        }
    }
}
 
int main() 
{
    int n ;
    scanf("%d" , &n) ;
    for ( int i = 1 ; i <= n ; i++ )
    {
        for ( int j = 1 ; j <= n ; j++ )
        {
            dis[i][j] = min( min( n + 1 - i , i ) , min( j , n + 1 - j ) ) 
            - 1 ;
            already[i][j] = true ;
        }
    }

    int res = 0 ;
    for ( int i = 1 ; i <= n * n ; i ++ )
    {
        int id ;
        scanf("%d" , &id) ;
        int x = ( id + n - 1 ) / n ;
        int y = id % n ;
        if ( y == 0 ) y += n ;
        res += dis[x][y] ;
        already[x][y] = false ;
        dfs( x , y ) ;
    }
    printf("%d\n" , res) ;
    return 0 ;
}