#pragma GCC optimize ("O2")
#pragma GCC target ("avx")
//#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;
#include<iostream>
#include<cstring>
#include<algorithm>

using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

ll A[200000], tmp[200001];
void pakuri_sort(int N, ll A[]) {
	const int b = 9;
	rep(k, 6) {
		int kazu[1 << b] = {}, kazu2[1 << b] = {};
		rep(i, N) kazu[A[i] >> k * b & ((1 << b) - 1)]++;
		rep(i, (1 << b) - 1) kazu[i + 1] += kazu[i];
		for (int i = N - 1; i >= 0; i--) tmp[--kazu[A[i] >> k * b & ((1 << b) - 1)]] = A[i];
		k++;
		rep(i, N) kazu2[tmp[i] >> k * b & ((1 << b) - 1)]++;
		rep(i, (1 << b) - 1) kazu2[i + 1] += kazu2[i];
		for (int i = N - 1; i >= 0; i--) A[--kazu2[tmp[i] >> k * b & ((1 << b) - 1)]] = tmp[i];
	}
}

const int CM = 1 << 17, CL = 20;
char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct;
const ll ma0 = 1157442765409226768;
const ll ma1 = 1085102592571150095;
const ll ma2 = 71777214294589695;
const ll ma3 = 281470681808895;
const ll ma4 = 4294967295;
const ll pre[9] = { 1,10,100,1000,10000,100000,1000000,10000000,100000000 };
inline ll getint() {
	if (ci - owa > 0) {
		memcpy(cn, owa, CL);
		ci -= CM;
		fread(cn + CL, 1, CM, stdin);
	}
	ll tmp = *(ll*)ci;
	if ((tmp & ma0) ^ ma0) {
		int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0);
		tmp = tmp << dig & ma1;
		tmp = tmp * 10 + (tmp >> 8) & ma2;
		tmp = tmp * 100 + (tmp >> 16) & ma3;
		tmp = tmp * 10000 + (tmp >> 32) & ma4;
		ci += (72 - dig >> 3);
	}
	else {
		tmp = tmp & ma1;
		tmp = tmp * 10 + (tmp >> 8) & ma2;
		tmp = tmp * 100 + (tmp >> 16) & ma3;
		tmp = tmp * 10000 + (tmp >> 32) & ma4;
		ci += 8;
		ll tmp2 = *(ll*)ci;
		if ((tmp2 & ma0) == ma0) {
			ci += 9;
			return 1000000000000000;
		}
		int dig = 68 - __builtin_ctzll((tmp2 & ma0) ^ ma0);
		if (dig == 64) {
			ci++;
			return tmp;
		}
		tmp2 = tmp2 << dig & ma1;
		tmp2 = tmp2 * 10 + (tmp2 >> 8) & ma2;
		tmp2 = tmp2 * 100 + (tmp2 >> 16) & ma3;
		tmp2 = tmp2 * 10000 + (tmp2 >> 32) & ma4;
		ci += (72 - dig >> 3);
		tmp = tmp * pre[64 - dig >> 3] + tmp2;
	}
	return tmp;
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);


	int N = getint();

	int K[3] = {};
	rep(i, N << 1) {
		ll a = getint();
		char c = *ci++;
		ci++;
		ll C = (ll(c) >> 4 | ll(c) << 1) & 3;
		K[C]++;
		A[i] = a | C << 50;
	}
	pakuri_sort(N * 2, A);
	if (K[0] % 2 == 0 && K[1] % 2 == 0 && K[2] % 2 == 0) {
		co(0);
		return 0;
	}

	auto P = A;
	auto Q = A + K[0];
	auto R = A + K[0] + K[1];
	const ll ma = (1ll << 50) - 1;
	for(int i = N * 2 - 1; i >= K[0]; i--) A[i] &= ma;
	ll sai[3] = {};
	ll saiq = 1e18, sair = 1e18;
	int q = 0, r = 0;
	rep(p, K[0]) {
		if (q < K[1]) if (saiq > abs(Q[q] - P[p])) saiq = abs(Q[q] - P[p]);
		while (q < K[1] - 1 && Q[q] < P[p]) {
			q++;
			if (saiq > abs(Q[q] - P[p])) saiq = abs(Q[q] - P[p]);
		}
		if (r < K[2]) if (sair > abs(R[r] - P[p])) sair = abs(R[r] - P[p]);
		while (r < K[2] - 1 && R[r] < P[p]) {
			r++;
			if (sair > abs(R[r] - P[p])) sair = abs(R[r] - P[p]);
		}
	}
	sai[2] = saiq;
	sai[1] = sair;
	sair = 1e18;
	r = 0;
	q = 0;
	while (q < K[1]) {
		if (r < K[2]) if (sair > abs(R[r] - Q[q])) sair = abs(R[r] - Q[q]);
		while (r < K[2] - 1 && R[r] < Q[q]) {
			r++;
			if (sair > abs(R[r] - Q[q])) sair = abs(R[r] - Q[q]);
		}
		q++;
	}
	sai[0] = sair;
	ll kotae = 1e18;
	if (K[0] % 2 == 0) kotae = min(sai[0], sai[1] + sai[2]);
	else if (K[1] % 2 == 0) kotae = min(sai[1], sai[2] + sai[0]);
	else kotae = min(sai[2], sai[0] + sai[1]);
	co(kotae);

	Would you please return 0;
}
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
char In[1 << 20], *ss = In, *tt = In;
#define getchar() (ss == tt && (tt = (ss = In) + fread(In, 1, 1 << 20, stdin), ss == tt) ? EOF : *ss++)
ll read() {
	ll x = 0, f = 1; char ch = getchar();
	for(; ch < '0' || ch > '9'; ch = getchar()) if(ch == '-') f = -1;
	for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + int(ch - '0');
	return x * f;
}
int readrgb() {
	char ch = getchar();
	for(; ch != 'R' && ch != 'G' && ch != 'B'; ch = getchar());
	return ch == 'R' ? 0 : (ch == 'G' ? 1 : 2);
}
const int MAXN = 1e5 + 5;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
int n;
vector<ll> ori[3];


ll getmindis(const vector<ll>& p, const vector<ll>& q) {
	if(!p.size() || !q.size()) return INF;
	ll ret = INF;
	for(int i = 0, j = 0; i < (int)p.size(); i++) {
		while(j < (int)q.size()-1 && q[j+1] <= p[i]) j++;
		if(q[j] <= p[i]) ret = min(ret, p[i] - q[j]);
	}
	for(int i = (int)p.size()-1, j = (int)q.size()-1; i >= 0; i--) {
		while(j > 0 && q[j-1] >= p[i]) j--;
		if(q[j] >= p[i]) ret = min(ret, q[j] - p[i]);
	}
	return ret;
}

int main() {
	n = read();
	for(int i = 1; i <= 2 * n; i++) {
		ll a = read(), c = readrgb();
		ori[c].push_back(a);
	}
	if((ori[0].size()&1) < (ori[1].size()&1)) swap(ori[0], ori[1]);
	if((ori[1].size()&1) < (ori[2].size()&1)) swap(ori[1], ori[2]);
	if((ori[0].size()&1) == 0) {
		printf("0\n");
		return 0;
	}
	sort(ori[0].begin(), ori[0].end());
	sort(ori[1].begin(), ori[1].end());
	sort(ori[2].begin(), ori[2].end());
	printf("%lld\n", min(getmindis(ori[0], ori[1]), getmindis(ori[0], ori[2]) + getmindis(ori[1], ori[2])));
	return 0;
}
