#include <bits/stdc++.h>
 
using namespace std;
 
#define MS(a, b) memset(a, b, sizeof(a))
#define REP(a, b, c) for (register int a = b, _n = c; a <= _n; ++a)
#define DREP(a, b, c) for (register int a = b, _n = c; a >= _n; --a)
#define FOR(a, b, c) for (register int a = b, _n = c; a < _n; ++a)
#define EREP(a, b) for (register int a = head[b]; a; a = edge[a].nxt)
#define ll long long
 
inline int rd() {
    int x = 0; char c = getchar(); bool f = 0;
    while (c < '0' && c != '-') c = getchar();
    if (c == '-') f = 1, c = getchar();
    while (c >= '0') x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
    return f ? -x : x;
}

const int SIZE = 1e5 + 100;

int n, m, t[3];
char Co[10];
vector<ll> V[3];

ll dp[SIZE], pre[SIZE], suf[SIZE];

void _main () {
    n = rd();
    REP (i, 1, 2 * n) {
        ll x;
        scanf ("%lld%s", &x, Co);
        if (Co[0] == 'R') V[0].push_back(x);
        else if (Co[0] == 'G') V[1].push_back(x);
        else V[2].push_back(x);
    }
    FOR (i, 0, 3) sort(V[i].begin(), V[i].end());
    int WTF = 0;
    FOR (i, 0, 3) if (V[i].size() & 1)
        t[WTF++] = i;
    if (WTF == 0) puts("0");
    else {
        n = V[t[0]].size(), m = V[t[1]].size();
        ll ans = 1e18;
        int x = 0;
        FOR (i, 0, n) {
            if (x) ans = min(ans, llabs(V[t[0]][i] - V[t[1]][x - 1]));
            while (x + 1 < m && V[t[1]][x] <= V[t[0]][i]) {
                ans = min(ans, llabs(V[t[0]][i] - V[t[1]][x]));
                ++x;
            }
            ans = min(ans, llabs(V[t[0]][i] - V[t[1]][x]));
        }

        t[2] = 3 ^ t[0] ^ t[1];
        int T = V[t[2]].size();
        if (T) {
            x = 0;
            FOR (i, 0, T) {
                dp[i] = 1e18;
                if (x) dp[i] = min(dp[i], llabs(V[t[2]][i] - V[t[1]][x - 1]));
                while (x + 1 < m && V[t[1]][x] <= V[t[2]][i]) {
                    dp[i] = min(dp[i], llabs(V[t[2]][i] - V[t[1]][x]));
                    ++x;
                }
                dp[i] = min(dp[i], llabs(V[t[2]][i] - V[t[1]][x]));
            }
            pre[0] = dp[0];
            FOR (i, 1, T) pre[i] = min(pre[i - 1], dp[i]);
            suf[T - 1] = dp[T - 1];
            DREP (i, T - 2, 0) suf[i] = min(suf[i + 1], dp[i]);

            x = 0;
            FOR (i, 0, T) {
                dp[i] = 1e18;
                if (x) dp[i] = min(dp[i], llabs(V[t[2]][i] - V[t[0]][x - 1]));
                while (x + 1 < n && V[t[0]][x] <= V[t[2]][i]) {
                    dp[i] = min(dp[i], llabs(V[t[2]][i] - V[t[0]][x]));
                    ++x;
                }
                dp[i] = min(dp[i], llabs(V[t[2]][i] - V[t[0]][x]));
                if (i) ans = min(ans, dp[i] + pre[i - 1]);
                if (i != T - 1) ans = min(ans, dp[i] + suf[i + 1]);
            }
        }

        // 0 1 2;
        printf ("%lld\n", ans);
    }
}

int main () {
    _main();
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;

#define rep(i, n) for(int i = 0, i##_len=(n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
#define len(x) ((int)(x).size())

void _cin() {} template <class Head, class... Tail> void _cin(Head&& head, Tail&&... tail) { cin >> head; _cin(forward<Tail>(tail)...); }
#define cin(Type, ...) Type __VA_ARGS__; _cin(__VA_ARGS__)
#define cinv(Type, xs, n) vector<Type> xs(n); rep(i, n) cin >> xs[i]
#define cinv2(Type, xs, ys, n) vector<Type> xs(n), ys(n); rep(i, n) cin >> xs[i] >> ys[i]
#define cinv3(Type, xs, ys, zs, n) vector<Type> xs(n), ys(n), zs(n); rep(i, n) cin >> xs[i] >> ys[i] >> zs[i]
#define cinvv(Type, xs, h, w) vector<vector<Type>> xs(h, vector<Type>(w)); rep(i, h) rep(j, w) cin >> xs[i][j]

void print() { cout << endl; }
template <class Head, class... Tail> void print(Head&& head, Tail&&... tail) { cout << head; if (sizeof...(tail) != 0) cout << " "; print(forward<Tail>(tail)...); }
template <class Type> void print(vector<Type> &vec) { for (auto& a : vec) { cout << a; if (&a != &vec.back()) cout << " "; } cout << endl; }
template <class Type> void print(vector<vector<Type>> &df) { for (auto& vec : df) { print(vec); } }

void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void yesno(bool b) { cout << (b ? "yes" : "no") << endl; }
template<class Integer>bool chmax(Integer &a, const Integer &b) { if (a < b) { a = b; return 1; } return 0; }
template<class Integer>bool chmin(Integer &a, const Integer &b) { if (b < a) { a = b; return 1; } return 0; }

using ll = long long;
using P = pair<int, int>;
int fs = 10000;
int n;
vector<int> x(200), y(200), r(200), a(200), b(200), c(200), d(200);
vector<int> id(200);

void show() {

}

void mset(int i, int _a, int _b, int _c, int _d) { a[i] = _a; b[i] = _b; c[i] = _c; d[i] = _d; }

int area(int i) { return (c[i] - a[i])*(d[i] - b[i]); }

bool check() {
	rep(i, n) {
		if (a[i] < 0 || fs < c[i] || b[i] < 0 || fs < d[i])return false;
		rep(j, n) {
			if (i == j)continue;
			if ((a[i] < c[j]) && (a[j] < c[i]) && (b[i] < d[j]) && (b[j] < d[i]))return false;
		}
	}
	return true;
}

void Main() {
	rep(i, n) {
		int pc = c[id[i]], pd = d[id[i]];
		int ok = 0, ng = fs;
		while (ng - ok > 1) {
			int wj = (ok + ng) / 2;
			c[id[i]] = pc + wj;
			d[id[i]] = pd + wj;
			if (check())ok = wj;
			else ng = wj;
		}
		c[id[i]] = pc + ok;
		d[id[i]] = pd + ok;
		if (r[id[i]] < area(id[i])) {
			int s = (int)sqrt(r[id[i]]);
			c[id[i]] = a[id[i]] + s;
			d[id[i]] = b[id[i]] + s;
		}
		
		int pa = a[id[i]], pb = b[id[i]];
		ok = 0, ng = fs;
		while (ng - ok > 1) {
			int wj = (ok + ng) / 2;
			a[id[i]] = pa - wj;
			b[id[i]] = pb - wj;
			if (check())ok = wj;
			else ng = wj;
		}
		a[id[i]] = pa - ok;
		b[id[i]] = pb - ok;
		if (r[id[i]] < area(id[i])) {
			int s = (int)sqrt(r[id[i]]);
			a[id[i]] = c[id[i]] - s;
			b[id[i]] = d[id[i]] - s;
		}
	}
}

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	//cout << fixed << setprecision(16);
	//int t; cin >> t; while (t--)
	cin >> n;
	vector<P> ri(n);
	rep(i, n) cin >> x[i] >> y[i] >> ri[i].first;
	rep(i, n)r[i] = ri[i].first;
	rep(i, n) ri[i].second = i;
	sort(ri.rbegin(), ri.rend());
	rep(i, n)id[i] = ri[i].second;
	rep(i, n) mset(i, x[i], y[i], x[i] + 1, y[i] + 1);
	Main();
	rep(i, n) {
		print(a[i], b[i], c[i], d[i]);
	}
	show();
}