#include <cstdio>
#include <cstring>
#include <cmath>
#include <cassert>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <iostream>
#include <numeric>

/* debug macros */
#ifdef WAFDAYO
#define DBG_LINE() {std::cerr<<"\e[2m[L"<<__LINE__<<"]\e[m ";}
#define DBG_PRINT(s,t,u) {std::cerr<<(s)<<" \e[2m=\e[m \e[1m"<<(t)<<"\e[m"<<(u);}
#define SELECT_7TH(x1,x2,x3,x4,x5,x6,x7,...) x7
#define dbg1(x1) DBG_PRINT(#x1,x1,std::endl)
#define dbg2(x1,x2) DBG_PRINT(#x1,x1,", ")dbg1(x2)
#define dbg3(x1,x2,x3) DBG_PRINT(#x1,x1,", ")dbg2(x2,x3)
#define dbg4(x1,x2,x3,x4) DBG_PRINT(#x1,x1,", ")dbg3(x2,x3,x4)
#define dbg5(x1,x2,x3,x4,x5) DBG_PRINT(#x1,x1,", ")dbg4(x2,x3,x4,x5)
#define dbg6(x1,x2,x3,x4,x5,x6) DBG_PRINT(#x1,x1,", ")dbg5(x2,x3,x4,x5,x6)
#define dbg(...) DBG_LINE()\
SELECT_7TH(__VA_ARGS__,dbg6,dbg5,dbg4,dbg3,dbg2,dbg1)(__VA_ARGS__)
#else
#define dbg(...) {}
#endif

/* utility functions */
struct read_item{read_item(){};
template<class T>operator T(){T t;std::cin>>t;return t;}};
char splf(int i,int n){return(i+1<n)?' ':'\n';};
template<bool up>
struct _RI{int i;_RI(int a):i(a){}
int operator*(){return i;}void operator++(){i+=(up?1:-1);}
bool operator!=(const _RI& r){return up?i<r.i:i>r.i;}};
template<bool up>
struct _RX{_RI<up> x,y;_RI<up> begin(){return x;}_RI<up> end(){return y;}
_RX(int a,int b):x(up?a:(b-1)),y(up?b:(a-1)){}_RX(int a):_RX(0,a){}};
typedef _RX<true> range;
typedef _RX<false> revrange;

/* types and constants */
typedef int64_t i64;
typedef uint64_t u64;
// const i64 inf = (i64)1.05e18;
// const int inf = (int)1.05e9;

using namespace std;

int main() {

	const int N = read_item();
	array<vector<i64>, 3> As;

	for(auto _ : range(2 * N)) {
		i64 a = read_item();
		string s = read_item();
		char c = s[0];
		int x = (c == 'R' ? 0 : c == 'G' ? 1 : 2);
		As[x].push_back(a);
	}

	vector<int> odds;
	for(auto i : range(3)) {
		if(As[i].size() % 2 == 1) {
			odds.push_back(i);
		}
	}

	assert(odds.size() == 0 || odds.size() == 2);

	if(odds.size() == 0) {
		printf("0\n");
		return 0;
	}

	auto xs = As[odds[0]];
	auto ys = As[odds[1]];
	auto zs = As[3 - odds[0] - odds[1]];

	sort(xs.begin(), xs.end());
	sort(ys.begin(), ys.end());
	sort(zs.begin(), zs.end());
	const int yn = ys.size();

	i64 ans = 1e18;

	auto nearest = [&](const vector<i64>& vs, i64 x) {
		int i = lower_bound(vs.begin(), vs.end(), x) - vs.begin();
		i = min(i, (int)vs.size() - 1);
		int j = max(i - 1, 0);
		return min(abs(x - vs[i]), abs(x - vs[j]));
	};

	for(auto x : xs) {
		i64 a = nearest(ys, x);
		ans = min(ans, a);
	}

	if(zs.size() >= 2) {

		vector<pair<i64, int>> zxs, zys;
		for(auto i : range(zs.size())) {
			auto z = zs[i];
			i64 zx = nearest(xs, z);
			i64 zy = nearest(ys, z);
			zxs.push_back({zx, i});
			zys.push_back({zy, i});
		}

		sort(zxs.begin(), zxs.end());
		sort(zys.begin(), zys.end());

		vector<i64> vs;
		for(auto i : range(2)) {
			for(auto j : range(2)) {
				auto zx = zxs[i];
				auto zy = zys[j];
				if(zx.second == zy.second) {
					continue;
				}
				vs.push_back(zx.first + zy.first);
			}
		}

		sort(vs.begin(), vs.end());
		i64 a = vs[0];
		ans = min(ans, a);
	}

	printf("%lld\n", ans);

	return 0;
}

/* wafdayo~~~ */

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_set>
using namespace std;
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define _rep(i, n) _rep2(i, 0, n)
#define _rep2(i, a, b) for(int i = (int)(a); i < (int)(b); i++)
#define rep(...) GET_MACRO(__VA_ARGS__, _rep2, _rep)(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using i64 = long long;
template<class T, class U>
bool chmin(T& a, const U& b) { return (b < a) ? (a = b, true) : false; }
template<class T, class U>
bool chmax(T& a, const U& b) { return (b > a) ? (a = b, true) : false; }

template<typename T>istream& operator>>(istream&i,vector<T>&v){rep(j,v.size())i>>v[j];return i;}
template<typename T>string join(vector<T>&v){stringstream s;rep(i,v.size())s<<' '<<v[i];return s.str().substr(1);}
template<typename T>ostream& operator<<(ostream&o,vector<T>&v){if(v.size())o<<join(v);return o;}
template<typename T>string join(vector<vector<T>>&vv){string s="\n";rep(i,vv.size())s+=join(vv[i])+"\n";return s;}
template<typename T>ostream& operator<<(ostream&o,vector<vector<T>>&vv){if(vv.size())o<<join(vv);return o;}

i64 solve(vector<i64>& a, vector<i64>& b, vector<i64>& c){
    sort(all(a));
    sort(all(b));
    i64 res = 5e18;
    for (auto i: a){
        auto x = lower_bound(all(b), i) - b.begin();
        if (x > 0) chmin(res, i - b[x - 1]);
        if (x < int(b.size())) chmin(res, b[x] - i);
    }
    vector<pair<i64, int>> aaa, bbb;
    for (auto i: c){
        i64 aa = 1e18, bb = 1e18;
        auto x = lower_bound(all(b), i) - b.begin();
        if (x > 0) chmin(bb, i - b[x - 1]);
        if (x < int(b.size())) chmin(bb, b[x] - i);
        x = lower_bound(all(a), i) - a.begin();
        if (x > 0) chmin(aa, i - a[x - 1]);
        if (x < int(a.size())) chmin(aa, a[x] - i);
        aaa.emplace_back(aa, i);
        bbb.emplace_back(bb, i);
    }
    sort(all(aaa));
    sort(all(bbb));
    if (aaa.empty()) return res;
    if (aaa[0].second == bbb[0].second) return min(res, min(aaa[0].first + bbb[1].first, aaa[1].first + bbb[0].first));
    return min(res, aaa[0].first + bbb[0].first);
}

int main()
{
    int n;
    cin >> n;
    vector<i64> R, G, B;
    rep(i, 2 * n){
        i64 a;
        char c;
        cin >> a >> c;
        if (c == 'R') R.emplace_back(a);
        if (c == 'G') G.emplace_back(a);
        if (c == 'B') B.emplace_back(a);
    }
    i64 ans = 0;
    if (R.size() & 1){
        if (B.size() & 1) ans = solve(R, B, G);
        else if (G.size() & 1) ans = solve(R, G, B);
    }
    else if ((B.size() & 1) && (G.size() & 1)) ans = solve(B, G, R);
    cout << ans << endl;
}