import numpy as np

def processInput():
    "正常動作を確認済み"
    line1 = input()
    line2 = input()
    N = int(line1)
    P = [int(digit) for digit in line2.split(" ")]
    return N, P
    

def makeTable(N):
    "正常動作を確認済み"
    return np.arange(1, N**2+1).reshape(N, N)


def num2Index(N, p_i):
    "正常動作を確認済み"
    row_idx = (p_i-1) // N
    col_idx = p_i % N -1
    return row_idx, col_idx


def markExistHuman(row_idx, col_idx):
    "人がいる場所に0を記入する"
    table[row_idx, col_idx] = 0    

def retMinDislike(N, p_i):
    row_idx, col_idx = num2Index(N, p_i)
    left_human = (table[row_idx, :col_idx]==0).sum()
    right_human = (table[row_idx, col_idx+1:]==0).sum()
    upper_human = (table[:row_idx, col_idx]==0).sum()
    lower_human = (table[row_idx+1:, col_idx]==0).sum()
    best_num = left_human
    if best_num > right_human:
        best_num = right_human
    elif best_num > upper_human:
        best_num = upper_human
    elif best_num > lower_human:
        best_num = lower_human
    markExistHuman(row_idx, col_idx)
    return best_num


def main():
  N, P = processInput()
  table = makeTable(N)
  P.reverse()
  min_total_dislike = 0
  for i in range(len(P)):
      min_total_dislike += retMinDislike(N, P[i])
  else:
      print(min_total_dislike)

main()
