package main

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

func main() {
	sc := NewScanner()

	n := sc.ReadInt()
	pCount := n * n
	nodes := make([]Node, pCount)
	for row := 0; row < n; row++ {
		for col := 0; col < n; col++ {
			i := n*row + col
			node := &nodes[i]
			node.number = i + 1

			if row > 0 {
				node.forward = &nodes[i-n]
			}
			if col > 0 {
				node.left = &nodes[i-1]
			}
			if row < n-1 {
				node.backward = &nodes[i+n]
			}
			if col < n-1 {
				node.left = &nodes[i+1]
			}

			node.cost = Min(row, col, n-1-row, n-1-col)
		}
	}

	totalCost := 0
	for pi := 0; pi < pCount; pi++ {
		nodeNum := sc.ReadInt()
		nodeExited := &nodes[nodeNum-1]
		totalCost += nodeExited.cost
		nodeExited.empty = true

		nodesToCheck := NewNodeQueue()
		nodesToCheck.Enqueue(nodeExited)
		for nodesToCheck.Len() > 0 {
			node := nodesToCheck.Dequeue()
			node.EachAround(func(nodeAround *Node) {
				var costChanged int
				if node.empty {
					costChanged = node.cost
				} else {
					costChanged = node.cost + 1
				}

				if costChanged < nodeAround.cost {
					nodeAround.cost = costChanged
					nodesToCheck.Enqueue(nodeAround)
				}
			})
		}
	}

	fmt.Println(totalCost)
}

type Node struct {
	number                         int
	empty                          bool
	cost                           int
	forward, backward, left, right *Node
}

func (node *Node) EachAround(callback func(*Node)) {
	aroundNodes := []*Node{node.forward, node.left, node.backward, node.right}
	for _, aroundNode := range aroundNodes {
		if aroundNode == nil {
			continue
		}
		callback(aroundNode)
	}
}

type NodeQueue struct {
	first *NodeQueueElem
	last  *NodeQueueElem
	len   int
}

type NodeQueueElem struct {
	child *NodeQueueElem
	value *Node
}

func NewNodeQueue() *NodeQueue {
	return &NodeQueue{nil, nil, 0}
}

func (q *NodeQueue) Len() int {
	return q.len
}

func (q *NodeQueue) Enqueue(item *Node) {
	node := NodeQueueElem{nil, item}
	if q.first == nil {
		q.first = &node
		q.last = &node
	} else {
		q.last.child = &node
		q.last = &node
	}
	q.len++
}

func (q *NodeQueue) Dequeue() *Node {
	elem := q.first
	q.first = elem.child
	q.len--
	return elem.value
}

// util
// *   math/simple

func Abs(x int) int {
	if x >= 0 {
		return x
	}
	return -x
}

func Min(values ...int) int {
	if len(values) == 0 {
		panic("no values")
	}
	min := values[0]
	for _, v := range values {
		if v < min {
			min = v
		}
	}
	return min
}

func Max(values ...int) int {
	if len(values) == 0 {
		panic("no values")
	}
	max := values[0]
	for _, v := range values {
		if v > max {
			max = v
		}
	}
	return max
}

func Pow(base int, exponent uint) int {
	answer := 1
	for i := uint(0); i < exponent; i++ {
		answer *= base
	}
	return answer
}

func Ceil(divident, dividor int) int {
	quo := divident / dividor
	rem := divident % dividor

	if rem != 0 {
		if (divident > 0 && dividor > 0) ||
			(divident < 0 && dividor < 0) {
			return quo + 1
		}
	}
	return quo
}

// *   sortutil

func ReverseInts(a []int) {
	sort.Sort(sort.Reverse(sort.IntSlice(a)))
}

func ReverseStrings(a []string) {
	sort.Sort(sort.Reverse(sort.StringSlice(a)))
}

// *   io

type Scanner struct {
	bufScanner *bufio.Scanner
}

func NewScanner() *Scanner {
	bufSc := bufio.NewScanner(os.Stdin)
	bufSc.Split(bufio.ScanWords)
	bufSc.Buffer(nil, 100000000)
	return &Scanner{bufSc}
}

func (sc *Scanner) ReadString() string {
	bufSc := sc.bufScanner
	bufSc.Scan()
	return bufSc.Text()
}

func (sc *Scanner) ReadInt() int {
	bufSc := sc.bufScanner
	bufSc.Scan()
	text := bufSc.Text()

	num, err := strconv.Atoi(text)
	if err != nil {
		panic(err)
	}
	return num
}
