#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][N],cnt[3*N];
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 = n; i >= 1; i--){
        for(int j = i+1; j <= n; j++){
            int poc = a[j],kraj = b[j];
            int ima = cnt[b[j]-1] - cnt[a[j]-1];
            //cout << i << " " << j << " " << poc << " " << kraj << " " << dp[i][j-ima-1] << " " << dp[j-ima][j-1] << " ima " << ima  << endl;
            dp[i][j] = dp[i][j-ima-1] + dp[j-ima][j-1];
            int pock = a[j-ima],krajk = b[j-ima];
            if(pock < i && krajk > j){
                dp[i][j]++;
            }
        }
    }
    return dp[1][n];
}
/*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;
}*/