// Template
#include "bits/stdc++.h"
#define rep_override(x, y, z, name, ...) name
#define rep2(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep3(i, l, r) for (int i = (int)(l); i < (int)(r); ++i)
#define rep(...) rep_override(__VA_ARGS__, rep3, rep2)(__VA_ARGS__)
#define per(i, n) for (ll i = (ll)(n) - 1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
constexpr int inf = 1001001001;
constexpr ll INF = 3003003003003003003LL;
template <typename T>
inline bool chmin(T &x, const T &y) {
    if (x > y) {
        x = y;
        return true;
    }
    return false;
}
template <typename T> 
inline bool chmax(T &x, const T &y) {
    if (x < y) {
        x = y;
        return true;
    }
    return false;
}
struct IOSET {
    IOSET() {
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(10);
    }
} ioset;

// Main
int main() {
    int n;
    cin >> n;
    vector<int> a(n), b(n);
    rep(i, n) {
        cin >> a[i] >> b[i];
        if (a[i] > 0) --a[i];
        if (b[i] > 0) --b[i];
    }

    vector<int> v(2 * n, -1);
    rep(i, n) {
        if (a[i] != -1) v[a[i]] = i;
        if (b[i] != -1) v[b[i]] = i + n;
    }

    int cnt = 0;
    rep(i, n) if (a[i] == -1 && b[i] == -1) ++cnt;
    auto dfs = [&](auto f, int now) -> void {
        // cout << now << "\n";
        if (now == 2 * n) {
            cout << "Yes\n";
            exit(0);
        }
        rep(i, 1, n + 1) {
            if (now + i * 2 > 2 * n) break;
            bool flag = true;
            int c = 0;
            rep(j, now, now + i) {
                if (v[j] >= n || (v[j + i] != -1 && v[j + i] < n)) {
                    flag = false;
                    break;
                }
                if (v[j] != -1) {
                    if (b[v[j]] != -1 && b[v[j]] != j + i) {
                        flag = false;
                        break;
                    }
                }
                if (v[j + i] != -1) {
                    if (a[v[j + i] - n] != -1 && a[v[j + i] - n] != j) {
                        flag = false;
                        break;
                    }
                }
                if (v[j] == -1 && v[i + j] == -1) ++c;
            }
            if (c > cnt) flag = false;
            if (!flag) continue;
            cnt -= c;
            f(f, now + 2 * i);
            cnt += c;
        }
    };
    dfs(dfs, 0);
    cout << "No\n";
}
#include<bits/stdc++.h>
using namespace std;

template<class T> void chmin(T &a,T b){if(a>b) a=b;};

int main(){
  int N;cin>>N;
  vector<long> x,y,z;
  for(int i=0;i<2*N;i++){
    long a;char c;cin>>a>>c;
    if(c=='R') x.push_back(a); 
    if(c=='G') y.push_back(a);
    if(c=='B') z.push_back(a);
  }
  int l,m,n,cnt;
  long X,Y,Z;X=Y=Z=(1L<<60);
  if(!(x.size()&1) && !(y.size()&1) && !(z.size()&1)){
    cout<<0<<endl;
    return 0;
  }
  
  if(!(x.size()&1)) swap(x,z);
  if(!(y.size()&1)) swap(y,z);
  
  //if((x.size()&1) && (y.size()&1)){
    sort(x.begin(),x.end());
    sort(y.begin(),y.end());
  sort(z.begin(),z.end());
    //if(x.size()>y.size()) swap(x,y);
    l=x.size();m=y.size();n=z.size();
    cnt=0;
    for(int i=0;i<l;i++){
      if(X==0) break;
      for(int j=cnt;j<m;j++){
        if(j==m-1){
          chmin(X,abs(x[i]-y[j]));
          break;
        }
        if(y[j]==x[i]){
          X=0;
          break;
        }
        if(y[j+1]<=x[i]){
          cnt++;
          continue;
        }
        chmin(X,abs(x[i]-y[j]));
        chmin(X,abs(x[i]-y[j+1]));
        break;
      }
    }
  cnt=0;
  for(int i=0;i<m;i++){
    if(Y==0) break;
      for(int j=cnt;j<n;j++){
        if(j==n-1){
          chmin(Y,abs(y[i]-z[j]));
          break;
        }
        if(z[j]==y[i]){
          Y=0;
          break;
        }
        if(z[j+1]<=y[i]){
          cnt++;
          continue;
        }
        chmin(Y,abs(y[i]-z[j]));
        chmin(Y,abs(y[i]-z[j+1]));
        break;
      }
    }
  cnt=0;
  for(int i=0;i<n;i++){
    if(Z==0) break;
      for(int j=cnt;j<l;j++){
        if(j==l-1){
          chmin(Z,abs(z[i]-x[j]));
          break;
        }
        if(x[j]==z[i]){
          Z=0;
          break;
        }
        if(x[j+1]<=z[i]){
          cnt++;
          continue;
        }
        chmin(Z,abs(z[i]-x[j]));
        chmin(Z,abs(z[i]-x[j+1]));
        break;
      }
    }
  cout<<min(X,Y+Z)<<endl;
}