#include <iostream>
#include <math.h>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <iomanip>
#include <set>
#include <bitset>
#include <bit>
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using piii = tuple<int,int,int>;
#define endl '\n'
#define f first
#define s second
int const N = 2e3+10;
int INF = 1e9+10;
int dp[N];
int MCM(int n,int acn,vector<int> vc){
if(dp[n] != -1) return dp[n];
// cout << n << endl;
// for(auto k:vc) cout << k << " ";
// cout << endl;
if(n <= 2) return dp[n] = 0;
else{
int ans = INF;
vector<pii> pos(n+1);
int i = 0;
for(int j{};j < acn;j++){
if(vc[j] != -1){
pos[vc[j]] = {j,i};
i++;
}
}
for(int i{n};i >= 1;i--){
vc[pos[i].f] = -1;
int cnt = 0;
set<int> del;
for(int j{i};j <= n;j++){
int val = n-pos[j].s-1;
//cout << n << " " << i << " " << j << " " << val << endl;
cnt += val;
if(!del.empty()){
auto it = del.lower_bound(val);
int dif = distance(del.begin(),it);
cnt -= dif;
}
del.insert(val);
}
//cout << n << " " << i << " " << cnt << endl;
ans = min(ans,MCM(i-1,acn,vc)+cnt);
}
//cout << "--" << n << "--" << ans << endl;
return dp[n] = ans;
}
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
memset(dp,-1,sizeof dp);
int n;cin >> n;
vector<int> vc(n);
for(int i{};i < n;i++){
cin >> vc[i];
}
cout << MCM(n,n,vc);
}