import scala.io.StdIn._
object Main extends App {

  val n = readInt()
  val nums = readLine.trim.split(' ').map(_.toInt).zipWithIndex.groupBy(_._1).toArray.sortBy(_._1).map(_._2.map(_._2))
  val countBit = new CountBit(n)
  var count = 0L
  for (indices ← nums) {
    for (i ← indices) countBit.increment(i)
    val rest = n - countBit.apply(n - 1)
    for (i ← indices) {
      val left = i + 1 - countBit(i)
      val right = rest - left
      count += Math.min(left, right)
    }
  }
  println(count)

  class CountBit(size: Int) {
    private val array: Array[Int] = Array.fill(size){0}
    def increment(position: Int): Unit = {
      if (position < size){
        array(position) += 1
        increment(position + ((position + 1) & ~position))
      }
    }
    def apply(position: Int): Int = {
      if (position >= 0){
        array(position) + apply(position - ((position + 1) & ~position))
      }else 0
    }
  }
}
