#include <bits/stdc++.h>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <set>
#include <utility>

#define MAX_N (2000)
#define SIZE_BUF (30)
#define INF (1000000000)

using namespace std;
using namespace __gnu_pbds;

typedef tree<pair<char, int>, null_type, less<pair<char, int>>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

int number_before_wss[MAX_N + 1][MAX_N + 1],
    number_before_bss[MAX_N + 1][MAX_N + 1];
int dpss[MAX_N + 1][MAX_N + 1];

int main(int argc, char *argv[]) {
  // read inputs
  int N, as[2 * MAX_N];
  char cs[2 * MAX_N], buf[SIZE_BUF];
  scanf("%d", &N);
  fgets(buf, SIZE_BUF, stdin);
  for (int i = 0; i < 2 * N; i++) {
    scanf("%c %d", &cs[i], &as[i]);
    as[i]--;  // NOTE : modified input
    fgets(buf, SIZE_BUF, stdin);
  }

  // record the index of each ball
  int kbs[MAX_N], kws[MAX_N];
  for (int k = 0; k < 2 * N; k++) {
    const char c = cs[k];
    const int a = as[k];
    if (c == 'B') {
      kbs[a] = k;
    } else {
      kws[a] = k;
    }
  }

  // num_before_wss[i + 1][j + 1] : the number of elements in B0~Bi and W0~Wj
  //                                that come before the element Wj in the
  //                                initial state
  // num_before_bss[i + 1][j + 1] : the number of elements in B0~Bi and W0~Wj
  //                                that come before the element Bi in the
  //                                initial state
  ordered_set balls;
  for (int k = 0; k < 2 * N; k++) {
    const char c = cs[k];
    const int a = as[k];
    if (c == 'W') {
      number_before_wss[0][a] = balls.order_of_key(make_pair('W', a)) -
                                balls.order_of_key(make_pair('W', 0));
      for (int i = 0; i < N; i++) {
        number_before_wss[i + 1][a] =
            number_before_wss[0][a] +
            balls.order_of_key(make_pair('B', i + 1)) -
            balls.order_of_key(make_pair('B', 0));
      }
    } else {
      number_before_bss[a][0] = balls.order_of_key(make_pair('B', a)) -
                                balls.order_of_key(make_pair('B', 0));
      for (int j = 0; j < N; j++) {
        number_before_bss[a][j + 1] =
            number_before_bss[a][0] +
            balls.order_of_key(make_pair('W', j + 1)) -
            balls.order_of_key(make_pair('W', 0));
      }
    }
    balls.insert(make_pair(c, a));
  }
  // printf("number_before_wss:\n");
  // for (int i = 0; i <= N; i++) {
  //   for (int j = 0; j <= N; j++) {
  //     printf("%d ", number_before_wss[i][j]);
  //   }
  //   printf("\n");
  // }
  // printf("number_before_bss:\n");
  // for (int i = 0; i <= N; i++) {
  //   for (int j = 0; j <= N; j++) {
  //     printf("%d ", number_before_bss[i][j]);
  //   }
  //   printf("\n");
  // }

  // solve dp
  // dpss[i][j] : the minimum number of moves needed to bring B0~Bi-1 and
  //              W0~Wj-1 to the first i + j elements.
  //              The order of the remaining 2 * N - (i + j) elements
  //              doesn't change from the initial state.
  // initialize
  dpss[0][0] = 0;
  for (int i = 0; i < N; i++) {
    dpss[i + 1][0] = dpss[i][0] + kbs[i] - number_before_bss[i][0];
  }
  for (int j = 0; j < N; j++) {
    dpss[0][j + 1] = dpss[0][j] + kws[j] - number_before_wss[0][j];
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      dpss[i + 1][j + 1] =
          min(dpss[i][j + 1] + kbs[i] - number_before_bss[i][j + 1],
              dpss[i + 1][j] + kws[j] - number_before_wss[i + 1][j]);
    }
  }
  // printf("dpss:\n");
  // for (int i = 0; i <= N; i++) {
  //   for (int j = 0; j <= N; j++) {
  //     printf("%d ", dpss[i][j]);
  //   }
  //   printf("\n");
  // }
  printf("%d\n", dpss[N][N]);

  return 0;
}


Measured memory_bytes: 47616