package main

import (
	"bufio"
	"fmt"
	"os"
)

func min(a ...int) int {
	ret := a[0]
	for i := 1; i < len(a); i++ {
		if ret > a[i] {
			ret = a[i]
		}
	}
	return ret
}

func main() {
	r := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscan(r, &n)
	n2 := n * n
	p := make([]int, n2)
	for i := 0; i < n2; i++ {
		fmt.Fscan(r, &p[i])
		p[i]--
	}

	a := make([]bool, n2)
	d := make([]int, n2)
	for i := 0; i < n2; i++ {
		x, y := i/n, i%n
		d[i] = min(x, y, n-x-1, n-y-1)
	}

	var ans int
	for i := 0; i < n2; i++ {
		ans += d[p[i]]
		a[p[i]] = true

		var q []int
		q = append(q, p[i])
		for len(q) > 0 {
			j := q[0]
			q = q[1:]
			for _, k := range []int{-n, n, -1, 1} {
				nj := j + k
				nx, ny := nj/n, nj%n
				if nx <= 0 || nx >= n-1 || ny <= 0 || ny >= n-1 {
					continue
				}
				if d[nj] <= d[j] {
					continue
				}
				d[nj] = d[j]
				if a[nj] {
					q = append(q, nj)
				}
			}
		}
	}
	fmt.Println(ans)
}
