import java.util.Scanner;publicclassMain_HDU1394{staticint n;staticint[] a =newint[5005];staticint[] sum =newint[5005<<2];publicstaticvoidmain(String[] args){
Scanner sc =newScanner(System.in);while(sc.hasNext()){
n = sc.nextInt();build(1,n,1);int res =0;for(int i=1;i<=n;i++){
a[i]= sc.nextInt();
res +=query(a[i]+1,n,1,n,1);update(a[i]+1,1,n,1);}int ans =99999999;for(int i=1;i<=n;i++){
res = res-a[i]+n-a[i]-1;//将a[i]换到后面 小于a[i]有a[i]个数 大于a[i]有n-a[i]-1个数 说明少了有a[i]个逆序数 多了n-a[i]-1个逆序数
ans = Math.min(ans, res);}
System.out.println(ans);}}publicstaticvoidbuild(int l,int r,int root){
sum[root]=0;if(l==r)return;int m =(l+r)>>1;build(l,m,root<<1);build(m+1,r,root<<1|1);}publicstaticvoidupdate(int x,int l,int r,int root){if(l==r){
sum[root]=1;//插入该数return;}int m =(l+r)>>1;if(x<=m)update(x,l,m,root<<1);elseupdate(x,m+1,r,root<<1|1);
sum[root]= sum[root<<1]+ sum[root<<1|1];}publicstaticintquery(int x,int y,int l,int r,int root){if(x<=l&&r<=y){return sum[root];//查询[a[i]+1,n]已经插入的个数}int m =(l+r)>>1;int res =0;if(x<=m)
res +=query(x,y,l,m,root<<1);if(y>m)
res +=query(x,y,m+1,r,root<<1|1);return res;}}