#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define min(a,b) ( (a)<(b) ? (a) : (b) )
const ll maxn=200005;
struct nod{
	ll a; char c;
	nod(){}
	nod(ll _a,char _c) {a=_a,c=_c;}
}a[maxn];
ll r[maxn],g[maxn],b[maxn];
ll totr,totg,totb;





inline void solve(ll *a,ll *b,ll *c,ll lena,ll lenb,ll lenc) {
	ll ans=LLONG_MAX;
	for(int i=1;i<=lena;++i) {
		ll j=lower_bound(b+1,b+lenb+1,a[i])-b;
		ans=min(ans,abs(b[j]-a[i]));
		--j; if(j>=1 && j<=lenb) ans=min(ans,abs(b[j]-a[i]));
		j+=2; if(j>=1 && j<=lenb) ans=min(ans,abs(b[j]-a[i]));
	}
	ll jie1=abs(a[1]-c[1]),jie2=abs(b[1]-c[1]);
	for(int i=1;i<=lenc;++i) {
		ll ja=lower_bound(a+1,a+lena+1,c[i])-a;
		ll jb=lower_bound(b+1,b+lenb+1,c[i])-b;
		jie1=min(jie1,abs(c[i]-a[ja]));
//		cout<<jie1<<endl;
		jie2=min(jie2,abs(c[i]-b[jb]));
//		cout<<jie2<<endl;
		--ja; --jb;
		if(ja>=1 && ja<=lena)
		jie1=min(jie1,abs(c[i]-a[ja]));
		if(jb>=1 && jb<=lenb)
		jie2=min(jie2,abs(c[i]-b[jb]));
		ja+=2; jb+=2;
		if(ja>=1 && ja<=lena)
		jie1=min(jie1,abs(c[i]-a[ja]));
		if(jb>=1 && jb<=lenb)
		jie2=min(jie2,abs(c[i]-b[jb]));
	}
//	cout<<ans<<endl;
//	cout<<jie1<<" "<<jie2<<endl;
//	cout<<(jie1+jie2)<<endl;
	ans=min(ans,jie1+jie2);
	cout<<ans<<endl;
	return;
}
ll n; 


int main()
{
	ios::sync_with_stdio(false);
	cin>>n;
	for(int i=1;i<=(n<<1);++i) {
		cin>>a[i].a>>a[i].c;
		if(a[i].c=='R') r[++totr]=a[i].a;
		if(a[i].c=='G') g[++totg]=a[i].a;
		if(a[i].c=='B') b[++totb]=a[i].a;
	}
	if(totr%2==0 && totb%2==0 && totg%2==0) {cout<<0<<endl; return 0;}
	sort(r+1,r+totr+1); sort(g+1,g+totg+1); sort(b+1,b+totb+1);
	if(totg%2==0) solve(r,b,g,totr,totb,totg);
	if(totb%2==0) solve(r,g,b,totr,totg,totb);
	if(totr%2==0) solve(g,b,r,totg,totb,totr);
	return 0;
}

#line 1 "main.cpp"
#include <bits/stdc++.h>
using namespace std;

// template {{{
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;

#define  range(i, l, r) for (i64 i = (i64)(l); i < (i64)(r); (i) += 1)
#define rrange(i, l, r) for (i64 i = (i64)(r) - 1; i >= (i64)(l); (i) -= 1)

#define  whole(f, x, ...) ([&](decltype((x)) container) { return (f)(  begin(container),  end(container), ## __VA_ARGS__); })(x)
#define rwhole(f, x, ...) ([&](decltype((x)) container) { return (f)( rbegin(container), rend(container), ## __VA_ARGS__); })(x)

#define debug(x) cerr << "(" << __LINE__ << ")" << #x << ": " << (x) << endl

constexpr i32 inf   = 1001001001;
constexpr i64 infll = 1001001001001001001ll;

constexpr i32 dx[] = {0, -1, 1, 0, -1, 1, -1, 1}; 
constexpr i32 dy[] = {-1, 0, 0, 1, -1, -1, 1, 1};

struct IoSetup { IoSetup(i32 x = 15){ cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(x); cerr << fixed << setprecision(x); } } iosetup;

template <typename T = i64> T input() { T x; cin >> x; return x; }

template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { range(i, 0, v.size()) { os << v[i] << (i + 1 != (int)v.size() ? " " : ""); } return os; } 
template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; }

template <typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p) { os << "(" << p.first << ", " << p.second << ")"; return os; }
template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; }

template <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); }
template <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); }

template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }
// }}}

void solve() {
  int n = input();

  vector< pair<i64, int> > vs(2 * n);
  for (auto &[a, c] : vs) {
    char ch;
    cin >> a >> ch;
    if (ch == 'R') c = 0;
    if (ch == 'G') c = 1;
    if (ch == 'B') c = 2;
  }

  whole(sort, vs);

  vector< int > p(3, -1);
  vector< vector< int > > ps;
  range(i, 0, 2 * n) {
    p[vs[i].second] = i;
    ps.emplace_back(p);
  }

  auto dp = make_vector(2 * n + 1, 4, 1 << 3, infll);
  dp[0][0][0] = 0;
  range(i, 1, 2 * n + 1) {
    auto [a, c] = vs[i - 1];

    // b : 000(0) -> c+1
    range(bit, 0, 1 << 3) {
      chmin(dp[i][c + 1][bit], dp[i - 1][0][bit]);
    }

    // b : c+1 -> 0
    range(bit, 0, 1 << 3) {
      range(b, 1, 4) {
        if (ps[i - 1][b - 1] == -1) continue;
        int idx = ps[i - 1][b - 1];
        chmin(dp[i][0][bit], dp[i - 1][b][bit] + a - vs[idx].first);
      }
    }

    range(b, 0, 4) {
      range(bit, 0, 1 << 3) {
        int msk = 1 << c;
        i64 cost = 0;
        if (bit & msk) {
          int idx = ps[i - 1][c];
          if (idx == -1) cost = infll;
          else cost = a - vs[idx].first;
        }

        chmin(dp[i][b][bit], dp[i - 1][b][bit ^ msk] + cost);
      }
    }
  }

  cout << dp[2 * n][0][0] << endl;
}

signed main() {
  solve();
}
