import java.lang.Integer.min
import java.util.*

fun readInt(): Int = readLine()!!.toInt()
fun readInts(): List<Int> = readLine()!!.split(" ").map { it.toInt() }

fun parseInt(): Int {
    var digit = System.`in`.read()
    while(digit <= 32) {
        digit = System.`in`.read()
    }

    val negative = digit == '-'.toInt()
    if (negative) {
        digit = System.`in`.read()
    }
    var res = 0
    while (digit in '0'.toInt()..'9'.toInt()) {
        res *= 10
        res += digit - '0'.toInt()
        digit = System.`in`.read()
    }
    return if (negative) -res else res
}

fun gcd(x : Int, y : Int) : Int {
    var a = x
    var b = y
    var r = 0
    while(b > 0) {
        r = a % b
        a = b
        b = r
    }
    return a
}

val dl = intArrayOf(-1, 0, 1, 0)
val dc = intArrayOf(0, 1, 0, -1)

fun calc(i : Int, j : Int, dp : Array<IntArray>, rem : Array<BooleanArray>) : Boolean {
    var newVal = Int.MAX_VALUE
    for(k in 0..3) {
        val x = i + dl[k]
        val y = j + dc[k]
        if(0 <= x && x < dp.size && 0 <= y && y < dp[0].size) {
            newVal = min(newVal, dp[x][y] + 1 - (if(rem[x][y]) 1 else 0))
        } else {
            newVal = 0
        }
    }

    if(newVal == dp[i][j]) return false

    dp[i][j] = newVal
    return true
}

fun main() {
//    val t = readLine()!!.toInt()
    val t = 1

    for(testNumber in 1..t) {
        val n = readInt()
        val p = readInts()
//        val n = 500
//        val p = IntArray(n * n) { i -> i + 1 }

        val dp = Array(n) { i -> IntArray(n) { j -> min(min(min(i, j), n - 1 - i), n - 1 - j) } }
        val rem = Array(n) { BooleanArray(n) { false } }
        val q = Array(n * n) { Pair(0, 0) }

        var ans = 0
        for(x in p) {
            val i = (x - 1) / n
            val j = (x - 1) % n
            ans += dp[i][j]
            rem[i][j] = true

            var l = 0
            var r = 0
            q[0] = Pair(i, j)

            while(l <= r) {
                val (a, b) = q[l++]
                for(k in 0..3) {
                    val c = a + dl[k]
                    val d = b + dc[k]
                    if(0 <= c && c < dp.size && 0 <= d && d < dp[0].size && calc(c, d, dp, rem)) {
                        q[++r] = Pair(c, d)
                    }
                }
            }
        }

        println(ans)
    }
}