#include <bits/stdc++.h>
#define V vector
#define L(i, j, k) for(int i = (j); i <= (k); i++)
#define R(i, j, k) for(int i = (j); i >= (k); i--)
#define all(x) x.begin(), x.end()
#define sz(a) ((int) a.size())
#define pb push_back
using namespace std;
typedef long long ll;
const int INF=1e9;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;cin>>n;
V<ll>h(n),pos(n+1);
L(i,0,n-1)cin>>h[i],pos[h[i]]=i;
V<ll>dp(n+1,INF);
dp[0]=0;
L(i,0,n-1){
L(j,i+1,n){//si quiero agregar el conjunto [i+1,j]
ll cost=0;
L(k,i+1,j){
L(l,1,i){
if(pos[k]<pos[l])cost++;
}
}
L(k,i+1,j){
L(l,k+1,j){
if(pos[k]<pos[l])cost++;
}
}
dp[j]=min(dp[j],dp[i]+cost);
}
}
cout<<dp[n]<<endl;
}