
class F(val size: Int) { // fenwick
    val a = IntArray(size + 2) { 0 }

    fun add(t: Int) {
        var x = t
        while (x <= size + 1) {
            a[x]++
            x += -x and x
        }
    }

    fun remove(t: Int) {
        var x = t
        while (x <= size + 1) {
            a[x]--
            x += -x and x
        }
    }

    fun query(t: Int): Int {
        var x = t
        var s = 0
        while (x != 0) {
            s += a[x]
            x = x and (x - 1)
        }
        return s
    }
}

fun main(args: Array<String>) {
    val f = F(10)
    val n = getInt()
    val p = getInts().map { it - 1 }

    val row = ArrayList<F>()
    val col = ArrayList<F>()
    repeat(n + 1) {
        val r = F(n)
        val c = F(n)
        repeat(n) { i ->
            r.add(i + 1)
            c.add(i + 1)
        }
        row.add(r)
        col.add(c)
    }

    var out = 0
    p.forEach {
        val r = it / n + 1
        val c = it % n + 1

        row[r].remove(c)
        col[c].remove(r)
        val a1 = row[r].query(c)
        val a2 = row[r].query(n) - row[r].query(c)
        val a3 = col[c].query(r)
        val a4 = col[c].query(n) - col[c].query(r)

        out += Math.min(Math.min(a1, a2), Math.min(a3, a4))
    }
    println(out)
}


fun getInt() = readLine()!!.toInt()
fun getInts() = readLine()!!.split(" ").map { it.toInt() }
fun getLong() = readLine()!!.toLong()
fun getLongs() = readLine()!!.split(" ").map { it.toLong() }
