#include <iostream>
#include <iomanip>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cassert>
#include <string>
#include <set>
#include <map>
#include <random>
#include <bitset>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <deque>
#include <queue>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

using namespace std;
using ll = long long;
using ul = unsigned long long;
using ld = long double;

ll md(ll i, vector<ll> &a) {
  auto it = lower_bound(all(a), i);
  ll ans = 1e18;
  if (it != a.end())
    ans = min(ans, *it - i);
  if (it != a.begin())
    ans = min(ans, i - *prev(it));
  return ans;
}

int main() {
  ios_base::sync_with_stdio(0); cin.tie(0);
  ll n;
  cin >> n;
  vector<vector<ll>> a(3);
  ll x;
  char c;
  string cc = "RGB";
  rep(i, 2 * n) {
    cin >> x >> c;
    a[cc.find(c)].push_back(x);
  }
  for (auto &p : a)
    sort(all(p));
  sort(all(a), [](const auto &p1, const auto &p2) { return p1.size() % 2 > p2.size() % 2; });
  if (a[0].size() % 2 == 0) {
    cout << 0;
    return 0;
  }
  ll ans = 1e18;
  for (ll i : a[0])
    ans = min(ans, md(i, a[1]));
  rep(t, 2) {
    ll cm = 1e18;
    for (ll i : a[2]) {
      ans = min(ans, cm + md(i, a[1]));
      cm = min(cm, md(i, a[0]));
    }
    swap(a[0], a[1]);
  }
  cout << ans;
}


#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair <int, int>
#define pll pair <ll, ll>
#define pci pair <char, int>
#define pld pair <ld, ld>
#define ppld pair <pld, pld>
#define ppll pair <pll, pll>
#define pldl pair <ld, ll>
#define vll vector <ll>
#define vvll vector <vll>
#define vpll vector <pll>
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define mll map <ll, ll>
#define fastmap gp_hash_table
#define cd complex <double>
#define vcd vector <cd>
#define PI 3.14159265358979
#pragma 03
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll INF = 1e18;
char col[] = {'R', 'G', 'B'};
ll suff_min[200005][3], pref_min[200005][3];
pair <ll, char> dogs[200005];
ll mn[3], mx[3], cnt[3];
ll get_id(char c){
	for (ll i = 0; i < 3; i++){
		if (c == col[i]) return i;
	}
}
int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	ll n; cin >> n;
	for (ll i = 0; i < 3; i++) mn[i] = INF;
	for (ll i = 1; i <= 2 * n; i++){
		cin >> dogs[i].fi >> dogs[i].se;
		cnt[get_id(dogs[i].se)]++;
	}
	if ((cnt[0] % 2 == 0) && (cnt[1] % 2 == 0) && (cnt[2] % 2 == 0)){
		cout << 0 << "\n"; return 0;
	}
	sort(dogs + 1, dogs + 2 * n + 1);
	ll ans = INF;
	pref_min[0][0] = pref_min[0][1] = pref_min[0][2] = INF;
	suff_min[2 * n + 1][0] = suff_min[2 * n + 1][1] = suff_min[2 * n + 1][2] = INF;
	for (ll i = 1; i <= 2 * n; i++){
		for (ll c = 0; c < 3; c++) pref_min[i][c] = pref_min[i - 1][c];
		ll id = get_id(dogs[i].se);
		if (cnt[id] & 1){
			for (ll c = 0; c < 3; c++){
				if (c == id) continue;
				if (mx[c] == 0) continue;
				if (cnt[c] & 1) ans = min(ans, dogs[i].fi - mx[c]);
				else pref_min[i][id] = min(pref_min[i][id], dogs[i].fi - mx[c]);
			}
		}
		else{
			for (ll c = 0; c < 3; c++){
				if (c == id) continue;
				if (mx[c] == 0) continue;
				pref_min[i][c] = min(pref_min[i][c], dogs[i].fi - mx[c]);
			}
		}
		mx[id] = max(mx[id], dogs[i].fi);
	}
	for (ll i = 2 * n; i >= 1; i--){
		for (ll c = 0; c < 3; c++) suff_min[i][c] = suff_min[i + 1][c];
		ll id = get_id(dogs[i].se);
		if (cnt[id] & 1){
			for (ll c = 0; c < 3; c++){
				if (c == id) continue;
				if (mn[c] == INF) continue;
				if (cnt[c] % 2 == 0) suff_min[i][id] = min(suff_min[i][id], mn[c] - dogs[i].fi);
			}
		}
		else{
			for (ll c = 0; c < 3; c++){
				if (c == id) continue;
				if (mn[c] == INF) continue;
				suff_min[i][c] = min(suff_min[i][c], mn[c] - dogs[i].fi);
			}
		}
		mn[id] = min(mn[id], dogs[i].fi);
	}
	for (ll s = 1; s < 2 * n; s++){
		for (ll c1 = 0; c1 < 3; c1++){
			for (ll c2 = 0; c2 < 3; c2++){
				if (c1 == c2) continue;
				ans = min(ans, pref_min[s][c1] + suff_min[s + 1][c2]);
			}
		}
	}
	cout << ans << "\n";
}