#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define fst first
#define snd second
#define pb push_back
#define SZ(x) (int)x.size()
#define ALL(x) x.begin(),x.end()
#define forn(i,a,b) for(int i = a; i<b;i++)
#define mset(a,v) memset(a,v,sizeof(a))
#define FIN ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
template< typename T >
using iset = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
const int MAXN = 5000+5;
ll n;
ll h[MAXN];
ll dp[MAXN];
ll myi[MAXN];
ll f(ll x){
ll &res = dp[x];
if(res!=-1) return res;
if(x==n+1) return res=0;
res=100000000000000000;
iset<ll> ind; forn(i,x,n+1) ind.insert(myi[i]);
ll aux = 0;
for(int i = n+1; i>=x+1; i--){
iset<ll> nind = ind;
for(int j = i-1; j>=x; j--){
aux+=nind.order_of_key(myi[j]);
nind.erase(nind.find(myi[j]));
}
ll paux = f(i);
//cout<<"Estando en "<<x<<": \n";
//cout<<" Llendo a "<<i<<" "<<aux<<"+"<<paux<<" "<<'\n';
res=min(res, aux+paux);
}
return res;
}
int main(){ FIN;
cin>>n;
forn(i,0,n) cin>>h[i], myi[h[i]]=i;
mset(dp,-1);
cout<<f(1)<<'\n';
return 0;
}