import numpy as np

n = int( input() )
p = [ int( px ) for px in input().split() ]
#print( p )

ans = int( 0 )
ex  = [ 1 ] * ( n*n )

for i in range( n*n ):
#    print( i )
#    dp = np.zeros( ( n, n ) )
#    for j in range( n ):
#        dp[ : ] = n*n
    dp = [ n*n ] * ( n*n )
    dp[ p[ i ] - 1 ] = 0
#    print( dp )

    for l in range( n*n ):
        for j in range( n*n ):
#            print( j % n )
            if j <= n-1:
                dp[ j ] = min( [ dp[ j ], dp[ j+n ] + ex[ j ] ] )
            if j % n == 0:
                dp[ j ] = min( [ dp[ j ], dp[ j+1 ] + ex[ j ] ] )
            if j % n == n-1:
                dp[ j ] = min( [ dp[ j ], dp[ j-1 ] + ex[ j ] ] )
            if j >= n*n-n:
                dp[ j ] = min( [ dp[ j ], dp[ j-n ] + ex[ j ] ] )
            if j >= n and j < n*n-n and j % n != 0 and j % n != n-1:
                dmin = min( [ dp[j-n], dp[j+n], dp[j-1], dp[j+1] ] )
                dp[ j ] = min( [ dp[ j ], dmin + ex[ j ] ] )

#    print( dp )
    hate = n*n
    for j, dpj in enumerate( dp ):
        if j <= n-1 or j >= n*n-n or j % n == 0 or j % n == n-1:
            hate = min( hate, dpj )

#    print( hate )
    ans = ans + hate

#    print( dp )
    ex[ p[ i ] - 1 ] = 0
#    print( ex )


print( ans )
            



