N = gets.chomp.to_i
P = gets.chomp.split.map(&:to_i)

$go = Array.new(N) { Array.new(N, 1000) }

N.times do |i|
  N.times do |j|
    $go[i][j] = [$go[i][j], j].min
    $go[i][N-j-1] = [$go[i][N-j-1], j].min
  end
end

N.times do |i|
  N.times do |j|
    $go[i][j] = [$go[i][j], i].min
    $go[N-i-1][j] = [$go[N-i-1][j], i].min
  end
end

$left = Array.new(N) { Array.new(N, false) }

ans = 0

def dec(i, j, count)
  if i > 0 && !$gone[i-1][j] # 上へ
    $gone[i-1][j] = true
    if $left[i-1][j]
      dec(i-1, j, count)
    else
      $go[i-1][j] = [$go[i-1][j], count].min
    end
  end

  if j > 0 && !$gone[i][j-1] # 左へ
    $gone[i][j-1] = true
    if $left[i][j-1]
      dec(i, j-1, count)
    else
      $go[i][j-1] = [$go[i][j-1], count].min
    end
  end

  if i < N-1 && !$gone[i+1][j] # 下へ
    $gone[i+1][j] = true
    if $left[i+1][j]
      dec(i+1, j, count)
    else
      $go[i+1][j] = [$go[i+1][j], count].min
    end
  end

  if j < N-1 && !$gone[i][j+1] # 右へ
    $gone[i][j+1] = true
    if $left[i][j+1]
      dec(i, j+1, count)
    else
      $go[i][j+1] = [$go[i][j+1], count].min
    end
  end
end

P.each do |p|
  i, j = p.divmod(N)
  if j == 0
    i -= 1
    j = N-1
  else
    j -= 1
  end
  count = $go[i][j]
  ans += count

  $left[i][j] = true
  $gone = Array.new(N) { Array.new(N, false) }
  dec(i, j, count)
end

p ans
