import sys

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

tin=lambda : map(int, input().split())
lin=lambda : list(map(int, input().split()))
mod=1000000007

#+++++

def main():
	n = int(input())
	nn = [[min(i+1,j+1,n-i,n-j) for j in range(n)] for i in range(n)]
	is_sit = [[True] * n for _ in range(n)]
	cc = [[0]*n for _ in range(n)]
	
	#b , c = tin()
	pl = lin()
	ans = 0
	for p in pl:
		x, y = (p-1) // n, (p-1) % n
		ans += nn[x][y]-1
		is_sit[x][y] = False
		nn[x][y]-=1
		q = [(x, y, nn[x][y])]
		while q:
			(x, y, v) = q.pop()
			for dx,dy in ([-1,0], [0,-1], [1,0], [0,1]):
				if x+dx < 0 or x +dx >= n or y + dy < 0 or y + dy >= n:
					continue
				iss = 1 if is_sit[x+dx][y+dy] else 0
				if nn[x+dx][y + dy] <= v + iss:
					continue
				nn[x+dx][y+dy] = v
				q.append((x+dx, y+dy, v+is_sit[x+dx][y+dy]))
		
	print(ans)
	#s = input()	
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)
