from math import ceil

n = int(input())
seats = [[1 for i in range(n)] for j in range(n)]

a = list(map(int, input().split()))

hate = 0

for i in range(n**2):
	num = a[i]
	y = ceil(num/n) - 1
	x = (num-1) % n

	seats[y][x] = 0
	if x == 0 or y == 0 or x == n-1 or y == n-1:
		continue
	#Horizontal - Right
	num1 = sum(seats[y][x:])
	#Horizontal - Left
	num2 = sum(seats[y][:x+1])
	#Vertical - Up
	s = 0
	for i in range(y):
		s += seats[i][x]	
	#Verctial - Down
	num4 = sum(seats[y:][x])
	hate += min(num1, num2, s, num4)


print(hate)