/*Lucky_Glass*/
#include <set>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>

typedef long long llong;
#define con(typ) const typ &

const int N = 1e5 + 10;
const llong INF = 0x3f3f3f3f3f3f3f3fll;

int n;
std::set<llong> exi[3];
std::vector<llong> ele[3];

llong aAbs(con(llong) k) { return k < 0 ? -k : k; }
llong nextTo(std::set<llong> &se, con(llong) val) {
  auto s = se.lower_bound(val);
  llong ret = INF;
  if ( s != se.end() ) ret = std::min(ret, aAbs(val - *s));;
  if ( s != se.begin() ) ret = std::min(ret, aAbs(val - *(--s)));
  return ret;
}
int main() {
  scanf("%d", &n), n *= 2;
  for ( int i = 1, k; i <= n; ++i ) {
    char typ[10] = ""; llong val;
    scanf("%lld%s", &val, typ);
    if ( typ[0] == 'R' ) k = 0;
    else if ( typ[0] == 'G' ) k = 1;
    else k = 2;
    exi[k].insert(val), ele[k].push_back(val);
  }
  int sp[3] = {-1, -1, -1};
  for ( int i = 0; i < 3; ++i )
    if ( ele[i].size() & 1 ) {
      if ( ~sp[0] ) sp[1] = i;
      else sp[0] = i;
    } else {
      sp[2] = i;
    }
  if ( sp[0] == -1 ) { printf("0\n"); return 0; }
  llong ans = INF;
  for ( llong i : ele[sp[0]] )
    ans = std::min(ans, nextTo(exi[sp[1]], i));
  std::pair<llong, int> mna[2], mnb[2];
  mna[0].first = mnb[0].first = mna[1].first = mnb[1].first = INF;
  for ( int i = 0; i < (int)ele[sp[2]].size(); ++i ) {
    llong val = nextTo(exi[sp[0]], ele[sp[2]][i]);
    if ( val < mna[0].first ) mna[1] = mna[0], mna[0] = std::make_pair(val, i);
    else if ( val < mna[1].first ) mna[1] = std::make_pair(val, i);
    val = nextTo(exi[sp[1]], ele[sp[2]][i]);
    if ( val < mnb[0].first ) mnb[1] = mnb[0], mnb[0] = std::make_pair(val, i);
    else if ( val < mnb[1].first ) mnb[1] = std::make_pair(val, i);
  }
  if ( mna[0].second != mnb[0].second ) {
    ans = std::min(ans, mna[0].first + mnb[0].first);
  } else {
    ans = std::min(ans, mna[0].first + mnb[1].first);
    ans = std::min(ans, mna[1].first + mnb[0].first);
  }
  printf("%lld\n", ans);
  return 0;
}
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define int ll
#define pb push_back
#define INF 1e18
#define MOD 1000000007
#define mp make_pair
const double PI=3.141592653589793238462643383279502884197169399375105820974944;
#define REP(i,n) for (int i = 0; i < n; i++)
#define FOR(i,a,b) for (int i = a; i < b; i++)
#define REPD(i,n) for (int i = n-1; i >= 0; i--)
#define FORD(i,a,b) for (int i = a; i >= b; i--)
#define remax(a,b) a = max(a,b)
#define remin(a,b) a = min(a,b)
#define umap unordered_map
#define pii pair<int,int>
#define F first
#define S second
#define mii map<int,int>
#define vi vector<int>
#define vvi vector<vi>
#define itr :: iterator it
#define all(v) v.begin(),v.end()
#define WL(t) while(t--)
#define gcd(a,b) __gcd((a),(b))
#define lcm(a,b) ((a)*(b))/gcd((a),(b))
#define out(x) cout << #x << " is " << x <<"\n"
#define FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;

int util(vi A, vi B)
{
 
    int a = 0, b = 0;
    int result = INF;
    while (a < A.size() && b < B.size())
    {
        if (abs(A[a] - B[b]) < result)
            result = abs(A[a] - B[b]);
        if (A[a] < B[b])
            a++;
 
        else
            b++;
    }
    return result;
}

void solve()
{
	int n, a;
	char c;
	cin>>n;
	vi r, g, b;
	REP(i, 2*n)
	{
		cin>>a>>c;
		if(c=='R')
		{
			r.pb(a);
		}
		else if(c=='G')
		{
			g.pb(a);
		}
		else
		{
			b.pb(a);
		}
	}
	sort(all(r));
	sort(all(g));
	sort(all(b));
	if(r.size()%2==0&&g.size()%2==0&&b.size()%2==0)
	{
		cout<<"0";
	}
	else if(r.size()%2==0)
	{
		int x=util(r, g);
		int y=util(r, b);
		int z=util(g, b);
		cout<<min(z, x+y);
	}
	else if(g.size()%2==0)
	{
		int x=util(g, r);
		int y=util(g, b);
		int z=util(r, b);
		cout<<min(z, x+y);
	}
	else
	{
		int x=util(b, r);
		int y=util(b, g);
		int z=util(r, g);
		cout<<min(z, x+y);
	}
}
signed main()
{   
	FastIO
	int t=1;
	//cin>>t;
	WL(t){
solve();
if(t!=0)
cout<<"\n";
}
return 0;
}  