#include <iostream>#include <cstdio>#include <cstring>#include <list>#include <algorithm>usingnamespacestd;
#define FIN freopen("data.in", "r", stdin);#define mem(a, v) memset(a,v,sizeof(a))typedeflonglong LL;
constint maxn = 5000 + 3;
int n;
int c[maxn];
int a[maxn];
int lowbit(int x) { return x & -x; }
void update(int x, int v){
for(int i = x; i <= maxn; i += lowbit(i)) c[i] += v;
}
int query(int x){
int s = 0;
for(int i = x; i; i -= lowbit(i)) s += c[i];
return s;
}
int main(){
#ifdef EXMY
FIN
#endif // EXMYwhile(scanf("%d", &n) != EOF){
mem(c, 0);
int ans = 0;
for(int i = 1; i <= n; ++i){
scanf("%d", a + i);
update(a[i]+1, 1);
ans += i - query(a[i]+1);
}
int tmp = ans;
for(int i = 1; i < n; ++i){
int small = query(a[i] + 1) - 1; // 小于a[i]的个数, (n - small - 1)就是大于a[i]的个数
tmp += -small + (n - small - 1);
ans = min(tmp, ans);
}
printf("%d\n", ans);
}
return0;
}