// Problem: B - RGB Matching
// Contest: AtCoder - NOMURA Programming Contest 2021(AtCoder Regular Contest 121)
// URL: https://atcoder.jp/contests/arc121/tasks/arc121_b
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int,int>;
#define pb push_back
#define mp make_pair
const double PI = 4*atan(1);
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int MOD = 1e9+7;
const int MAX_N = 2e6+10;

int n;
ll a[MAX_N];
char c[MAX_N];

void solve()
{
	map<int,vector<ll>> x;
	for(int i=0;i<2*n;++i){
		if(c[i]=='R') x[0].pb(a[i]);
		else if(c[i]=='B')x[1].pb(a[i]);
		else x[2].pb(a[i]);
	}
	vector<int> res;
	for(int i=0;i<3;++i){
		sort(x[i].begin(),x[i].end());
		if((int)x[i].size()%2) res.pb(i);
	}
	if(res.empty()){
		cout<<"0\n";
		return;
	}
	int u = res[0], v = res[1];
	auto cal = [&] (int u, int v) {
		ll ans = LINF;
		if(x[v].empty()) return ans;
		for(int i=0,j=0;i<(int)x[u].size();++i){
			while(j+1<(int)x[v].size() && x[v][j+1]<=x[u][i]) j++;
			ans = min(ans, abs(x[u][i]-x[v][j]));
			if(j+1<(int)x[v].size()){
				ans = min(ans, abs(x[u][i]-x[v][j+1]));
			}
		}
		return ans;
	};
	int w = 3-u-v;
	ll ans = min(cal(u,v),cal(u,w)+cal(v,w));
	cout<<ans;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
	cin >> n;
	for(int i=0;i<2*n;++i)cin>>a[i]>>c[i];
	solve();
	return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define int long long 
const int N = 20;
int x[N], y[N], z[N];
int g[N][N];
int f[N][1 << N], n;
int get_dis(int a, int b) {
	return abs(x[a] - x[b]) + abs(y[a] - y[b]) + max(0ll, z[b] - z[a]);
}
signed main() {
#ifndef ONLINE_JUDGE
	freopen("E:\\in.txt", "r", stdin);
#endif
	cin >> n;
	int M = 1 << (n - 1);
	memset(f, 0x3f, sizeof f);
	for (int i = 0; i < n; i++) {
		cin >> x[i] >> y[i] >> z[i];
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			g[i][j] = get_dis(i, j);
		}
	}
	for (int i = 0; i < n; i++) {
		f[i][0] = g[i][0];
	}
	for (int j = 1; j < M; j++) {
		for (int i = 0; i < n; i++) {
			if ((j >> (i - 1)) & 1) continue;
			for (int k = 1; k < n; k++) {
				if (i == k) continue;
				if ((j >> (k - 1) & 1) == 0) continue;
				f[i][j] = min(f[i][j], g[i][k] + f[k][j ^ (1 << (k - 1))]);
			}
		}
	}
	cout << f[0][M - 1] << endl;
}