n = int(input())
p = list(map(int, input().split()))
n_list = []
n_reverse_list = []
for i in range(1, n+1):
  n_list.append(list(range((i-1)*n+1, i*n+1)))
  n_reverse_list.append(list(range(i, n**2+i+1-n, n)))
ans = 0
for i in p:
  row = (i - 1)//n
  column = (i - 1)%n
  n_list[row][column] = 0
  n_reverse_list[row][column] = 0
  a = len(n_list[row][:column]) - n_list[row][:column].count(0)
  b = len(n_list[row][column+1:]) - n_list[row][column+1:].count(0)
  c = len(n_reverse_list[row][:column]) - n_reverse_list[row][:column].count(0)
  d = len(n_reverse_list[row][column+1:]) - n_reverse_list[row][column+1:].count(0)
  ans += min(a, b, c, d)
print(ans)