import sys
import numpy as np

def input():
    return sys.stdin.readline()[:-1]


N = int(input())
P = list(map(int, input().split()))

SEAT = np.ones((N, N))
outer = [i for i in range(N)] + [i for i in range(N**2-N, N**2)]\
	+ [i*N for i in range(1, N-1)] + [i*N-1 for i in range(2, N)]
outer = set(outer)
flag = [[] for i in range(N**2)]
#print(outer)
#print(P)
#print('SEAT\n', SEAT)


def add_outer(num, pre=[]):
	i,j = [num//N, num%N]
	pre.append(num)
	
	if i < N-1: # 下
		outer.add(num+N)
		if SEAT[i+1, j] == 0 and num+N not in pre:
			add_outer(num+N, pre)
	if i > 0: # 上		
		outer.add(num-N)
		if SEAT[i-1, j] == 0 and num-N not in pre:
			add_outer(num-N, pre)
	if j < N-1: # 右
		outer.add(num+1)
		if SEAT[i, j+1] == 0 and num+1 not in pre:
			add_outer(num+1, pre)
	if j > 0: # 左
		outer.add(num-1)
		if SEAT[i, j-1] == 0 and num-1 not in pre:
			add_outer(num-1, pre)
			
			
ans = 0
for num in P:
	num -= 1
	i,j = [num//N, num%N]
	SEAT[i, j] = 0
	#print(SEAT)

	if num in outer:	
		add_outer(num)
	else:
		ans += min(sum(SEAT[:i, j]), sum(SEAT[i:, j]), sum(SEAT[i, :j]), sum(SEAT[i, j:]))
		#print(num, min(sum(SEAT[:i, j]), sum(SEAT[i:, j]), sum(SEAT[i, :j]), sum(SEAT[i, j:])))
		

print(int(ans))
