#include <bits/stdc++.h>
#define _CRT_SECURE_NO_WARNINGS
using namespace std;
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define uniq(x) x.resize(unique(all(x)) - x.begin());
#define sort_uniq(x) sort(all(x)), uniq(x);
#define ll long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define V vector
#define V2dll V<V<ll>>
#define V2dint V<V<int>>
#define V2dchar V<V<char>>
#define V2dbool V<V<bool>>
#define V3dll V<V<V<ll>>>
#define V3dint V<V<V<int>>>
#define V3dchar V<V<V<char>>>
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define eb emplace_back
#define FASTIO \
ios_base::sync_with_stdio(false); \
cin.tie(nullptr); \
cout.tie(nullptr);
#define INF INT32_MAX
#define blt __builtin_popcount
#define clr(x) x.clear()
#define ff first
#define ss second
#define popf pop_front
#define popb pop_back
#define sz(x) int(x.size())
#define rep(a, b, c, d) for (int a = b; a <= c; a += d)
#define repl(a, b, c, d) for (int a = b; a >= c; a -= d)
mt19937_64 rng(chrono::steady_clock().now().time_since_epoch().count());
const int N = 1e4 + 5;
int dp[N];
struct fenwick
{
int f[N];
fenwick()
{
rep(i, 0, N - 1, 1)
{
f[i] = 0;
}
}
void add(int pos, int v)
{
rep(i, pos, N - 1, (i & -i))
{
f[i] += v;
}
}
int get(int pos)
{
int ans = 0;
repl(i, pos, 1, (i & -i))
{
ans += f[i];
}
return ans;
}
};
int main()
{
FASTIO
int n;
cin >> n;
V<int> p(n + 1), pos(n + 1);
rep(i, 1, n, 1)
{
cin >> p[i];
pos[p[i]] = i;
}
fenwick ign, cf;
rep(i, 1, n, 1)
{
ign.add(i, 1);
}
dp[0] = dp[1] = 0;
ign.add(pos[1], -1);
rep(i, 2, n, 1)
{
ign.add(pos[i], -1);
int cur = 0;
dp[i] = 1e9;
repl(j, i, 1, 1)
{
cur += i - (pos[j] - ign.get(pos[j]));
cur -= cf.get(pos[j]);
dp[i] = min(dp[i], cur + dp[j - 1]);
cf.add(pos[j], 1);
}
repl(j, i, 1, 1) cf.add(pos[j], -1);
}
cout << dp[n] << "\n";
}