#include <cstdio>
#include <vector>
#include <deque>
#include <map>
using namespace std;

typedef long long i64;

struct BIT {
	vector<int> dat;
	int size;
	void init(int n) {
		size = n;
		dat.resize(size + 1);
		fill(dat.begin(), dat.end(), 0);
	}
	void add(int pos, int val) {
		for(int x = pos + 1; x <= size; x += x & -x) {
			dat[x] += val;
		}
	}
	int sum(int right) {
		int s = 0;
		for(int x = right; x > 0; x -= x & -x) {
			s += dat[x];
		}
		return s;
	}
};

int find_minlen(int pos, int n, BIT& remains)
{
	int a = remains.sum(pos);
	int b = remains.sum(n) - (a + 1);
	return min(a, b);
}

int main()
{
	int n;
	vector<int> vs;

	scanf("%d", &n);
	vs.resize(n);
	for(int i = 0; i < n; ++i) {
		scanf("%d", &vs[i]);
	}

	map<int,deque<int>> nums;
	BIT remains;

	remains.init(n);
	for(int i = 0; i < n; ++i) {
		remains.add(i, 1);
	}

	for(int i = 0; i < n; ++i) {
		nums[vs[i]].push_back(i);
	}

	i64 ans = 0;

	for(auto& pa : nums) {

		int v = pa.first;
		auto& pos_list = pa.second;

		while(pos_list.size() > 0) {
			int f = pos_list.front();
			int b = pos_list.back();
			int fv = find_minlen(f, n, remains);
			int bv = find_minlen(b, n, remains);
			if(fv < bv) {
				pos_list.pop_front();
				ans += fv;
				remains.add(f, -1);
			} else {
				pos_list.pop_back();
				ans += bv;
				remains.add(b, -1);
			}
		}
	}

	printf("%lld\n", ans);

	return 0;
}

