// Arup Guha
// 3/6/2021
// Solution to 2020 SER Problem: Bitonic Ordering

import java.util.*;
import java.io.*;

public class bitonic {

	public static void main(String[] args) throws Exception {
	
		BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
		
		int n = Integer.parseInt(stdin.readLine());
		int[] vals = new int[n];
		int[] copy = new int[n];
		for (int i=0; i<n; i++) {
			vals[i] = Integer.parseInt(stdin.readLine());
			copy[i] = vals[i];
		}
		
		// Create the mapping for the ordering.
		HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
		Arrays.sort(copy);
		for (int i=0; i<n; i++) map.put(copy[i], i+1);
		
		// Coordinate compress.
		for (int i=0; i<n; i++)
			vals[i] = map.get(vals[i]);
			
		// Store locations of each item 1 to n.
		int[] loc = new int[n+1];
		for (int i=0; i<n; i++)
			loc[vals[i]] = i+1;
		
		// Set up our bit. Put 1 in each spot.
		bit mybit = new bit(n+1);
		for (int i=1; i<=n; i++) mybit.add(i, 1); 
		
		long res = 0;
		
		for (int i=1; i<=n; i++) {
			int place = loc[i];
			int prev = place-1 >= 1 ? mybit.query(1,place-1) : 0;
			int next = place+1 <= n ? mybit.query(place+1,n) : 0;
			res = res + Math.min(prev, next);
			mybit.add(place, -1);
		}
		
		// Ta da!
		System.out.println(res);
	}
}

class bit {
	
	public int n;
	public int[] vals;
	
	public bit(int orign) {
		
		// Set i to be the first power of 2 >= n+1.
		n = 1;
		while (n < orign+1) n <<= 1;
		
		// Alloc space.
		vals = new int[n];
	}
	
	// Adds x to index idx of array.
	public void add(int idx, int x) {
		while (idx < n) {
			vals[idx] += x;
			idx += (idx&(-idx));
		}
	}
	
	// Returns sum of array[1..x]
	public int sum(int x) {
		int res = 0;
		while (x > 0) {
			res += vals[x];
			x -= (x&(-x));
		}
		return res;
	}
	
	// Returns sum of bit from a to b inclusive.
	public int query(int a, int b) {
		if (a == 1) return sum(b);
		return sum(b) - sum(a-1);
	}
}