#include <iostream>
#include <stdio.h>
#define MAX_N 5001
#define INF 99999999
using namespace std;
int a[MAX_N], n;
struct node{
int left, right, val;
} tt[MAX_N * 3];
void build(int left, int right, int index) {
tt[index].left = left;
tt[index].right = right;
tt[index].val = 0;
if (left == right) return;
int mid = (left + right) / 2;
build(left, mid, index * 2);
build(mid + 1, right, index * 2 + 1);
}
int query(int index, int left, int right) {
if (tt[index].left == left && tt[index].right == right)
return tt[index].val;
int mid = (tt[index].left + tt[index].right) / 2;
if (mid >= right) return query(index * 2, left, right);
if (mid < left) return query(index * 2 + 1, left, right);
return query(index * 2, left, mid) + query(index * 2 + 1, mid + 1, right);
}
void update(int index, int id) {
tt[index].val++;
if (tt[index].left == tt[index].right) return;
int mid = (tt[index].left + tt[index].right) / 2;
if (id <= mid) update(index * 2, id);
else update(index * 2 + 1, id);
}
int main() {
int i;
while (scanf("%d", &n) != EOF) {
build(0, n - 1, 1);
int sum = 0;
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
sum += query(1, a[i], n - 1);
update(1, a[i]);
}
int output = sum;
for (i = 0; i < n; i++) {
sum = sum - a[i] + (n - a[i] - 1);
if (sum < output) output = sum;
}
printf("%d\n", output);
}
return 0;
}