#include <bits/stdc++.h>
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;
 
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vvvi = vector<vector<vector<int>>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vvvl = vector<vector<vector<ll>>>;
using vs = vector<string>;
using vb = vector<bool>;
using vvb = vector<vector<bool>>;
#define FOR(i, m, n) for (int i = (m); i < (n); i++)
#define FORR(i, m, n) for (int i = (m); i >= (n); i--)
#define REP(i, n) FOR(i, 0, (n))
#define REPR(i, n) FORR(i, (n) - 1, 0)
#define REP1(i, n) FOR(i, 1, (n) + 1)
#define REPS(c, s) for (char c : s)
#define ALL(c) (c).begin(), (c).end()
#define SORT(c) sort(ALL(c))
#define REV(c) reverse(ALL(c))
#define sz(v) (int)v.size()
#define endl '\n'
template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b; return true;} return false;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b; return true;} return false;}
template<class T> inline void prn(vector<T>& v) {int n = sz(v); REP(i, n) cout << v[i] << ' ';}
template<class T> inline void printv(vector<T>& v) {int n = sz(v); REP(i, n) cout << v[i] << (i == n - 1 ? endl : ' ');}
template<class T> inline void printvv(vector<vector<T>>& v) {for (auto u : v) printv(u);}
template<class T> inline void printvm(vector<T>& v) {int n = sz(v); REP(i, n) cout << v[i].val() << (i == n - 1 ? endl : ' ');}
template<class T> inline void printvvm(vector<vector<T>>& v) {for (auto u : v) printvm(u);}
template<class T> inline void printlnv(vector<T>& v) {int n = sz(v); REP(i, n) cout << v[i] << endl;}
const int MOD = 1000000007;
const int INF = 1000000001;
const ll LINF = 1000000001000000001LL;
 
void solve();
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(numeric_limits<double>::max_digits10);
    solve();
    return 0;
}

void solve() {
    int n;
    cin >> n;
    ll a;
    char c;
    vl r, g, b;
    REP(i, n * 2) {
        cin >> a >> c;
        if (c == 'R') r.emplace_back(a);
        else if (c == 'G') g.emplace_back(a);
        else if (c == 'B') b.emplace_back(a);
    }

    SORT(r);
    SORT(g);
    SORT(b);

    if (sz(r) % 2 == 0 and sz(g) % 2 == 0 and sz(b) % 2 == 0) {
        cout << 0 << endl;
        return;
    }

    ll rg = LINF;
    for (auto e : g) {
        auto it = lower_bound(ALL(r), e);
        if (it == r.end()) chmin(rg, abs(r[sz(r) - 1] - e));
        else if (it == r.begin()) chmin(rg, abs(r[0] - e));
        else {
            int idx = distance(r.begin(), it);
            chmin(rg, abs(r[idx - 1] - e));
            chmin(rg, abs(r[idx] - e));
        }
    }
    ll rb = LINF;
    for (auto e : b) {
        auto it = lower_bound(ALL(r), e);
        if (it == r.end()) chmin(rb, abs(r[sz(r) - 1] - e));
        else if (it == r.begin()) chmin(rb, abs(r[0] - e));
        else {
            int idx = distance(r.begin(), it);
            chmin(rb, abs(r[idx - 1] - e));
            chmin(rb, abs(r[idx] - e));
        }
    }
    ll gb = LINF;
    for (auto e : b) {
        auto it = lower_bound(ALL(g), e);
        if (it == g.end()) chmin(gb, abs(g[sz(g) - 1] - e));
        else if (it == g.begin()) chmin(gb, abs(g[0] - e));
        else {
            int idx = distance(g.begin(), it);
            chmin(gb, abs(g[idx - 1] - e));
            chmin(gb, abs(g[idx] - e));
        }
    }

    if (sz(r) % 2 == 0) cout << min(rg + rb, gb) << endl;
    else if (sz(g) % 2 == 0) cout << min(rg + gb, rb) << endl;
    else if (sz(b) % 2 == 0) cout << min(rb + gb, rg) << endl;
}


#include<bits/stdc++.h>
#define SZ(x) ((int)x.size())
#define pb push_back
template <typename _Tp>void read(_Tp &x){
	char ch(getchar());bool f(false);while(!isdigit(ch))f|=ch==45,ch=getchar();
	x=ch&15,ch=getchar();while(isdigit(ch))x=x*10+(ch&15),ch=getchar();
	if(f)x=-x;
}
template <typename _Tp,typename... Args>void read(_Tp &t,Args &...args){read(t);read(args...);}
template <typename _Tp>inline void chmax(_Tp &a,const _Tp &b){a=a<b?b:a;}
template <typename _Tp>inline void chmin(_Tp &a,const _Tp &b){a=a<b?a:b;}
const int N=100005;
typedef long long ll;
std::vector<ll> a[3];
ll last[3];
ll calc(const std::vector<ll> &a,const std::vector<ll> &b){
	std::vector<std::pair<ll,int>> v;
	for(auto it:a)v.pb({it,0});
	for(auto it:b)v.pb({it,1});
	std::sort(v.begin(),v.end());
	last[0]=last[1]=-1e18;
	ll ans=1e18;
	for(auto [x,y]:v)chmin(ans,x-last[!y]),last[y]=x;
	return ans;
}
int main(){
	int n;read(n);
	for(int i=1;i<=n+n;++i){
		ll x;read(x);
		char ch;do{ch=getchar();}while(!isalpha(ch));
		a[ch=='R'?0:ch=='G'?1:2].pb(x);
	}
	if(SZ(a[0])%2==0&&SZ(a[1])%2==0&&SZ(a[2])%2==0)return puts("0"),0;
	int p=-1;for(int i=0;i<3;++i)if(SZ(a[i])%2==0)p=i;
	std::vector<ll> x,y,z=a[p];
	if(p==0)x=a[1],y=a[2];
	else if(p==1)x=a[0],y=a[2];
	else if(p==2)x=a[0],y=a[1];
	printf("%lld\n",std::min(calc(x,y),calc(x,z)+calc(y,z)));
	return 0;
}