import sys
import numpy as np
 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


N = int(readline())
P = np.array(list(map(int, readline().split())))

data = np.ones((N,N))
ans = 0


# 座ってる場所のindex判定
def seat_index(seat_num):
  div,mod = divmod(seat_num, N)
  if mod % N == 0:
    col = div-1
    row = N-1
  else:
    col = div
    row = mod-1
  return [col,row]


# 嫌う人数判定
def check(col, row):
  check_list = np.array([0,0,0,0])
  # 左
  check_list[0] = np.sum(data[col,:row])
  # 右
  check_list[1] = np.sum(data[col,row+1:])
  # 上
  check_list[2] = np.sum(data[:col,row])
  # 下
  check_list[3] = np.sum(data[col+1:,row])
  
  global ans
  ans += np.min(check_list)
  data[col][row] = 0


for i in range(len(P)):
  seat_num = P[i]
  col,row = seat_index(seat_num)
  check(col,row)


print(ans)




