#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 vc[N];
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n;cin >> n;
for(int i{};i < n;i++){
cin >> vc[i];
}
dp[2] = 0;
for(int t{3};t <= n;t++){
dp[t] = INF;
vector<pii> pos(t+1);
int i = 0;
for(int j{};j < n;j++){
if(vc[j] <= t){
pos[vc[j]] = {j,i};
i++;
}
}
for(int i{t};i >= 1;i--){
int cnt = 0;
set<int> del;
for(int j{i};j <= t;j++){
int val = t-pos[j].s-1;
cnt += val;
if(!del.empty()){
auto it = del.lower_bound(val);
int dif = distance(del.begin(),it);
cnt -= dif;
}
del.insert(val);
}
dp[t] = min(dp[t],dp[i-1]+cnt);
}
}
cout << dp[n];
}