#include <bits/stdc++.h>
using namespace std;

template <typename T> struct Node {
    T x;
    Node<T> *l, *r;
    Node() { l = nullptr; r = nullptr; x = T(); }
    Node(T px) { Node(); x = px; }
    T get(){ return x; }
    void set(T px) { x = px; }
    void add(T px) { x = x + px; }
    void update() {
        x = T();
        if(l) x = x + l->get();
        if(r) x = x + r->get();
    }
};
template <typename T, int L = 0, int R = 100100>
struct seg_tree {
    using node = Node<T>;
    virtual node* get(node* before) {
        return before? before : new node();
    }
    node* root;
    seg_tree() { clear(); }
    void clear() { root = nullptr; }
    node* set(node* cur, int l, int r, int pos, T x) {
        cur = get(cur);
        if(l == r) cur->set(x);
        else {
            int mid = (l+r) >> 1;
            if(pos <= mid) 
                cur->l = set(cur->l, l, mid, pos, x);
            else 
                cur->r = set(cur->r, mid+1, r, pos, x);
            cur->update();
        }
        return cur;
    }
    node* set(int pos, T x) {
        return root = set(root, L, R, pos, x);
    }
    node* add(node* cur, int l, int r, int pos, T x) {
        cur = get(cur);
        if(l == r) cur->add(x);
        else {
            int mid = (l+r) >> 1;
            if(pos <= mid) 
                cur->l = add(cur->l, l, mid, pos, x);
            else 
                cur->r = add(cur->r, mid+1, r, pos, x);
            cur->update();
        }
        return cur;
    }
    node* add(int pos, T x) {
        return root = add(root, L, R, pos, x);
    }
    T query(node* cur, int l, int r, int x, int y) {
        if(cur == nullptr || l > y || r < x) return T();
        if(x <= l && r <= y) return cur->get();
        int mid = (l+r) >> 1;
        if(y <= mid) 
            return query(cur->l, l, mid, x, y);
        if(x > mid) 
            return query(cur->r, mid+1, r, x, y);
        return query(cur->l, l, mid, x, y) + query(cur->r, mid+1, r, x, y);
    }
    T query(int x, int y) {
        return query(root, L, R, x, y);
    }
};

template<typename T, int L = 0, int R = 100100>
struct persistent_tree : seg_tree<T, L, R>{
    using node = Node<T>;
    node* get(node* before) override {
        node* new_node = new node();
        if(before) {
            new_node->l = before->l;
            new_node->r = before->r;
            new_node->x = before->x;
        }
        return new_node;
    }
};

const int MAX = 4222;
const int INF = 20000000;
using tree = persistent_tree<int,0,MAX>;

tree t[2][MAX];
char c[MAX];
int v[MAX],pos[2][MAX];
int dp[MAX][MAX];
int n;

int solve(int cur, int w) // pos, white
{
    if(cur == 2*n) return 0;
    if(dp[cur][w] != -1) return dp[cur][w];
    int b = cur - w, ans = INF;
    if(w != n) 
    {
        int nxt = pos[0][w+1]
                  + t[0][w].query(pos[0][w+1], MAX-1)
                  + t[1][b].query(pos[0][w+1], MAX-1);
        ans = min(ans, nxt - cur + solve(cur+1, w+1));
    }
    if(b != n) 
    {
        int nxt = pos[1][b+1]
                  + t[0][w].query(pos[1][b+1], MAX-1)
                  + t[1][b].query(pos[1][b+1], MAX-1);
        ans = min(ans, nxt - cur + solve(cur+1, w));
    }
    return dp[cur][w] = ans;
}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(0);
    while(cin >> n) 
    {
        for(int i = 0; i < 2*n; i++) 
        {
            cin >> c[i] >> v[i];
            c[i] = (c[i] == 'B');
            pos[c[i]][v[i]] = i;
        }
        for(int f = 0; f < 2; f++)
            for(int i = 1; i <= n; i++)
            {
                t[f][i].root = t[f][i-1].root;
                t[f][i].set(pos[f][i], 1);
            }
        memset(dp, -1, sizeof dp);
        cout << solve(0,0) << '\n';
    }
    return 0;
}

Measured memory_bytes: 72704