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

ll a[N],b[N],n;
ll sta[3*N];

ll 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];
        sta[a[i]] = i;
        sta[b[i]] = -i;
    }
    deque <ll> q;
    ll maxb = 0,sol = 0;
    for(int i = 0; i < 2*n; i++){
        if(sta[i] > 0){ /// neki pocetak
             ll kraj = b[sta[i]];
             //cout << i << " " << sta[i] << " " << kraj << endl;
             if(kraj > maxb){ /// ide desno
                 //cout << "desno" << endl;
                maxb = kraj;
                q.push_back(sta[i]);
             }
             else{ /// ide levo
                 //cout << "levo" << endl;
                q.push_front(sta[i]);
             }
        }
        else{ /// neki kraj
            ll dod = 0;
            deque <ll> q1;
            while(!q.empty() && q.front() != -sta[i]){
                q1.push_front(q.front());
                //cout << "izabcujem " << q.front() << endl;
                q.pop_front();
                dod++;
            }
            q.pop_front();
        //cout << dod << endl;
            sol += dod;
            while(!q1.empty()){
                q.push_front(q1.front());
                q1.pop_front();
            }
        }
    }
    return sol;
}
/*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;
}*/