#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<unordered_map>
#include<climits>
#include<cstdlib>
#include<cmath>
#include<string>
#include<iomanip>
#include<bitset>

using namespace std;

#define INF 1 << 29
#define LL long long int

LL const MOD = 1000000007;

template<typename T>
class SegmentTree{
    int n;
    int realsize;
    T initialvalue;
    vector<T> tree;

    public:
    
    SegmentTree(int size,T initial):realsize(size),initialvalue(initial){
        n = 1;
        while(n < size)n *= 2;

        tree = vector<T>(2*n + 1,initial);
    }

    //1-indexed
    void insert(int i,T data){
        int ind = n+i-1;
        tree[ind] = data;
        ind >>= 1;
        while(ind > 0){
            tree[ind] = tree[ind*2] + tree[ind*2+1];
            ind >>= 1;
        }
    }

    void add(int i,T data){
        int ind = n+i-1;
        tree[ind] += data;
        ind >>= 1;
        while(ind > 0){
            tree[ind] = tree[ind*2] + tree[ind*2+1];
            ind >>= 1;
        }
    }

    T get(int l,int r){
        if(l > realsize || r > realsize || l <= 0 || r <= 0)return initialvalue;
        l = n+l-1;
        r = n+r-1;
        if(l == r)return tree[l];
        int prel = l;
        int prer = r;
        T lnum = tree[l];
        T rnum = tree[r];
        l >>= 1;
        r >>= 1;
        while(l != r){
            if(l*2 == prel){
                lnum += tree[l*2+1];
            }
            if(r*2+1 == prer){
                rnum += tree[r*2];
            }
            prel = l;
            prer = r;
            l >>= 1;
            r >>= 1;
        }
        return lnum+rnum;
    }
};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    LL n;
    cin >> n;
    vector<int> bidx(n),widx(n);

    for(int i = 1; i <= 2*n; i++){
        char x;
        cin >> x;
        int y;
        cin >> y;
        if(x == 'B'){
            bidx[y-1] = i;
        }else{
            widx[y-1] = i;
        }
    }

    SegmentTree<int> st(2*n,0);

    vector<vector<int>> costb(n+1,vector<int>(n+1,0)),costw(n+1,vector<int>(n+1,0));
    for(int i = 0; i < n+1; i++){
        for(int j = 0; j < n; j++){
            costb[i][j] = st.get(bidx[j],2*n);
            st.add(bidx[j],1);
        }
        for(int j = 0; j < n; j++){
            st.insert(bidx[j],0);
        }
        if(i < n){
            st.add(widx[i],1);
        }
    }


    SegmentTree<int> st2(2*n,0);

    for(int i = 0; i < n+1; i++){
        for(int j = 0; j < n; j++){
            costw[i][j] = st2.get(widx[j],2*n);    
            st2.add(widx[j],1);
        }
        for(int j = 0; j < n; j++){
            st2.insert(widx[j],0);
        }
        if(i < n){
            st2.add(bidx[i],1);
        }
    }

    vector<vector<int>> dp(n+1,vector<int>(n+1,0));

    for(int i = 0; i < n+1; i++){
        for(int j = 0; j < n+1; j++){
            int x = 133333333;
            int y = 133333333;
            if(i == 0 && j == 0){
                continue;
            }
            if(i > 0){
                x = dp[i-1][j] + costb[j][i-1];
            }
            if(j > 0){
                y = dp[i][j-1] + costw[i][j-1];
            }
            dp[i][j] = min(x,y);
        }
    }

    cout << dp[n][n] << endl;

    return 0;
}

Measured memory_bytes: 47488