#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <ll,ll> p;
const int N = 1e3+34;

int a[N],b[N];
int dp[N][2],cnt[3*N]; /// 0 = levo,1 = desno
p nesto[N];
int n;

bool cmp(p x,p y){
    return x.second < y.second;
}

int Resi(int N,int *A,int *B){
    n = N;
    for(int i = 1; i <= n; i++){
        a[i] = A[i-1];
        b[i] = B[i-1];
        cnt[b[i]]++;
        nesto[i] = p(a[i],b[i]);
    }
    for(int i = 1; i <= 2*n; i++){
        cnt[i] += cnt[i-1];
    }
    sort(nesto+1,nesto+n+1,cmp);
    for(int i = 1; i <= n; i++){
        a[i] = nesto[i].first;
        b[i] = nesto[i].second;
    }
    for(int i = 1; i <= n; i++){
        /// desno stavi
        dp[i][1] = min(dp[i-1][0],dp[i-1][1]);
        for(int j = i+1; j <= n; j++){
            if(a[j] < b[i] && b[j] > b[i]){
                dp[i][1]++;
            }
        }
        /// levo stavi
        dp[i][0] = min(dp[i-1][0],dp[i-1][1]);
        for(int j = i-1; j >= 1; j--){
            if(a[j] < a[i] && b[j] > a[i]){
                dp[i][0]++;
            }
        }
    }
    return min(dp[n][0],dp[n][1]);
}
/*int main(){
    int n1[4],n2[4];
    n1[0] = 0;
    n1[1] = 1;
    n1[2] = 2;
    n1[3] = 5;
    n2[0] = 3;
    n2[1] = 7;
    n2[2] = 4;
    n2[3] = 6;
    cout << Resi(4,n1,n2) << endl;
}*/