import numpy as np
n = int(input())
p = list(map(int, input().split()))
n_array = np.arange(1, n**2 + 1).reshape(n, n)
ans = 0
for i in p:
  row = (i - 1)//n
  column = (i - 1)%n
  n_array[row, column] = 0
  a = np.count_nonzero(n_array[row, :column], axis=0)
  b = np.count_nonzero(n_array[row, column+1:], axis=0)
  c = np.count_nonzero(n_array[:row, column], axis=0)
  d = np.count_nonzero(n_array[row+1:, column], axis=0)
  ans += min(a, b, c, d)
print(ans)