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

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


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


	int N;
	cin >> N;

	ll A[200000];
	int K[3] = {};
	rep(i, N << 1) {
		ll a;
		char c;
		cin >> a >> c;
		ll C = (ll(c) >> 4 | ll(c) << 1) & 3;
		K[C]++;
		A[i] = a | C << 50;
	}
	sort(A, A + N * 2);
	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;
	rep(i, N << 1) 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<algorithm>
#include<cstdio>
#define ll long long
#define MaxN 200500
using namespace std;
const ll INF=1ll<<60;
struct Data{ll x;char c;}a[MaxN];
bool cmp(const Data &A,const Data &B)
{return A.x<B.x;}
int n,o[255];ll sav[255];
int main()
{
  scanf("%d",&n);n*=2;
  for (int i=1;i<=n;i++){
    scanf("%lld%s",&a[i].x,&a[i].c);
    o[a[i].c]++;
  }
  bool fl=0;
  for (int i='A';i<='Z';i++)if (o[i]&1)fl=1;
  if (!fl){puts("0");return 0;}
  sort(a+1,a+n+1,cmp);
  for (int i='A';i<='Z';i++)sav[i]=INF;
  ll ans1=INF,ans2=0;
  for (int i=1;i<=n;i++)if (o[a[i].c]&1){
    if (i>1&&a[i-1].c!=a[i].c){
      sav[a[i].c]=min(sav[a[i].c],a[i].x-a[i-1].x);
      int p=i-1;while(p>1&&!(o[a[p].c]&1))p--;
      if (o[a[p].c]&1&&a[p].c!=a[i].c)ans1=min(ans1,a[i].x-a[p].x);
    }
    if (i<n&&a[i+1].c!=a[i].c){
      sav[a[i].c]=min(sav[a[i].c],a[i+1].x-a[i].x);
      int p=i+1;while(p<n&&!(o[a[p].c]&1))p++;
      if (o[a[p].c]&1&&a[p].c!=a[i].c)ans1=min(ans1,a[p].x-a[i].x);
    }
  }
  for (int i='A';i<='Z';i++)
    if (o[i]&1)ans2+=sav[i];
  printf("%lld",min(ans1,ans2));
  return 0;
} 