package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}
func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}
func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}
func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}
func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}
func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0
	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 2
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}
	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	solve(scanner, writer)
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	n := getNextInt(scanner)
	decv := make([]decomposition, n)
	dech := make([]decomposition, n)
	for i := 0; i < n; i++ {
		decv[i] = newDecomposition(n)
		dech[i] = newDecomposition(n)
		for j := 0; j < n; j++ {
			decv[i].add(j, 1)
			dech[i].add(j, 1)
		}
	}
	ans := 0
	for i := 0; i < n*n; i++ {
		p := getNextInt(scanner) - 1
		y := p / n
		x := p % n
		decv[x].add(y, -1)
		dech[y].add(x, -1)
		min := n - 1
		min = minint(min, decv[x].rangesum(0, y))
		min = minint(min, decv[x].rangesum(y+1, n))
		min = minint(min, dech[y].rangesum(0, x))
		min = minint(min, dech[y].rangesum(x+1, n))
		ans += min
	}
	fmt.Fprintln(writer, ans)
}

func minint(a, b int) int {
	if a < b {
		return a
	}
	return b
}

type decomposition struct {
	chunk  int
	bucket [][]int
}

func newDecomposition(n int) decomposition {
	bucket := make([][]int, 1)
	bucket[0] = make([]int, n)
	chunk := 8
	for i := 0; n > 1; i++ {
		n = (n-1)/chunk + 1
		bucket = append(bucket, make([]int, n))
	}
	return decomposition{
		chunk:  chunk,
		bucket: bucket,
	}
}
func (dec *decomposition) upleft(l int) int {
	return (l + dec.chunk - 1) / dec.chunk
}
func (dec *decomposition) upright(r int) int {
	return r / dec.chunk
}
func (dec *decomposition) add(index int, value int) {
	h := len(dec.bucket)
	for i := 0; i < h; i++ {
		dec.bucket[i][index] += value
		index /= dec.chunk
	}
}
func (dec *decomposition) rangeadd(l, r int, value int) {
	dec.rangeaddh(l, r, 0, value)
}
func (dec *decomposition) rangeaddh(l, r, h int, value int) {
	ll := dec.upleft(l)
	rr := dec.upright(r)
	for i := l; i < r && i < ll*dec.chunk; i++ {
		dec.bucket[h][i] += value
	}
	if ll < rr {
		dec.rangeaddh(ll, rr, h+1, value)
	}
	for i := rr * dec.chunk; i < r && ll <= rr; i++ {
		dec.bucket[h][i] += value
	}
}
func (dec *decomposition) rangesum(l, r int) int {
	if l >= r {
		return 0
	}
	return dec.rangesumh(l, r, 0)
}
func (dec *decomposition) rangesumh(l, r, h int) int {
	var sum int
	ll := dec.upleft(l)
	rr := dec.upright(r)
	for i := l; i < r && i < ll*dec.chunk; i++ {
		sum += dec.bucket[h][i]
	}
	if ll < rr {
		sum += dec.rangesumh(ll, rr, h+1)
	}
	for i := rr * dec.chunk; i < r && ll <= rr; i++ {
		sum += dec.bucket[h][i]
	}
	return sum
}
