from collections import deque
n = int(input())
p = list(map(int, input().split()))

d = [[-1 for _ in range(n)] for _ in range(n)]
used = [[False for _ in range(n)] for _ in range(n)]
for i in range(n):
	for j in range(n):
		d[i][j] = min(i, j, n-1-i, n-1-j)
#for e in d:
#	print(*e)

DI = [1, 0, -1, 0]
DJ = [0, 1, 0, -1]

ans = 0
for x in p:
	x -= 1
	i, j = x//n, x%n
	ans += d[i][j]
	used[i][j] = True
	q = deque()
	for di, dj in zip(DI, DJ):
		if not ((0 <= i+di < n) and (0 <= j+dj < n)):
			continue
		if d[i+di][j+dj] > d[i][j]:
			d[i+di][j+dj] = d[i][j]
			q.append((i+di)*n + j+dj)
	while q:
		l = q.popleft()
		i, j = l//n, l%n
		for di, dj in zip(DI, DJ):
			if not ((0 <= i+di < n) and (0 <= j+dj < n)):
				continue
			if used[i][j]:
				if d[i+di][j+dj] > d[i][j]:
					d[i+di][j+dj] = d[i][j]
					q.appendleft((i+di)*n + j+dj)
			else:
				if d[i+di][j+dj] > d[i][j] + 1:
					d[i+di][j+dj] = d[i][j] + 1
					q.append((i+di)*n + j+dj)
	#print("----")
	#for e in d:
		#print(*e)

print(ans)