import java.util.*

private class B {
    fun solve() {
        val n = nextInt();
        val p = Array(n * n) { nextInt() - 1 }
        val hall = Array(n) { IntArray(n) { 1 } } 
        val hatred = Array(n) { x -> IntArray(n) {y -> x.coerceAtMost(y).coerceAtMost(n - x - 1).coerceAtMost(n - y - 1)} }
        var ans = 0
        for (current in p) {
            val q = LinkedList<Point>()
            val x = current % n
            val y = current / n
            q.add(Point(x, y))
            hall[x][y] = 0
            ans += hatred[x][y]
            while (!q.isEmpty()) {
                val current = q.pollFirst()
                val nextHatred = hatred[current.x][current.y] + hall[current.x][current.y]
                for (i in 0..3) {
                    val next = current.shift(i)
                    if (next.isGood(n) && nextHatred < hatred[next.x][next.y]) {
                        hatred[next.x][next.y] = nextHatred
                        q.addLast(next)
                    }
                }
            }
        }
        println(ans)
    }

    class Point(val x: Int, val y: Int) {
        val dx = intArrayOf(-1, 1, 0, 0)
        val dy = intArrayOf(0, 0, -1, 1)

        fun shift(dir: Int): Point {
            return Point(x + dx[dir], y + dy[dir])
        }

        fun isGood(n: Int): Boolean {
            return x >= 0 && x < n && y >= 0 && y < n
        }
    }


    var tokenBuffer = listOf<String>()
    var bufferPosition = 0

    private fun checkBuffer() {
        if (bufferPosition == tokenBuffer.size) {
            tokenBuffer = nextLine().split(" ")
            bufferPosition = 0
        }
    }

    fun nextLine() = readLine()!!
    fun nextInt(): Int {
        checkBuffer()
        return tokenBuffer[bufferPosition++].toInt()
    }

    fun nextLong(): Long {
        checkBuffer()
        return tokenBuffer[bufferPosition++].toLong()
    }

    fun nextDouble(): Double {
        checkBuffer()
        return tokenBuffer[bufferPosition++].toDouble()
    }

    fun nextToken(): String {
        checkBuffer()
        return tokenBuffer[bufferPosition++]
    }
}

fun main() {
    val reader = B().solve()

}
