// Created by ash_98
 
#include<bits/stdc++.h>
using namespace std;
 
#define mx 200005
#define ll long long
#define mod 1000000007
 
int ar[mx];
char ch[mx];
int n,m,ii,k;

pair<ll,int> chk(vector<ll>c,vector<ll>b,int bat)
{
    vector<ll>a;
    int sz=c.size();
    for(int i=0;i<sz;i++)
    {
        if(i!=bat)a.push_back(c[i]);
    }
    int sz1=a.size();
    int sz2=b.size();
    ll re=1e18;
    int id=-1;
    for(int i=0;i<sz1;i++)
    {
        int be=0,en=sz2-1;
        int shuru=-1;
        while(be<=en)
        {
            int mid=(be+en)/2;
            if(b[mid]<a[i])
            {
                shuru=mid;
                be=mid+1;
            }
            else en=mid-1;
        }
        if((shuru+1)<sz2)
        {
            ll val=abs(a[i]-b[shuru+1]);
            if(val<re)
            {
                re=val;
                id=i;
            }
        }
        if(shuru!=-1)
        {
            ll val=abs(a[i]-b[shuru]);
            if(val<re)
            {
                re=val;
                id=i;
            }
        }

    }

    return {re,id};
}

ll func(vector<ll>a,vector<ll>b,vector<ll>c)
{
    ll re=chk(a,b,-1).first;

    int sz3=c.size();
    if(sz3<2)return re;

    pair<ll,int>re1=chk(c,a,-1);
    pair<ll,int>re2=chk(c,b,re1.second);

    re=min(re,re1.first+re2.first);

    re1=chk(c,b,-1);
    re2=chk(c,a,re1.second);

    re=min(re,re1.first+re2.first);


   return re;


}
 
void solve()
{
    vector<ll>R,B,G;
    scanf("%d",&n);
    n*=2;
    for(int i=1;i<=n;i++)
    {
        ll x;
        char cc;
        cin>>x>>cc;
        if(cc=='R')R.push_back(x);
        else if(cc=='B')B.push_back(x);
        else G.push_back(x);
    }
    int sz1=R.size();
    int sz2=B.size();
    int sz3=G.size();
    sort(R.begin(),R.end());
    sort(G.begin(),G.end());
    sort(B.begin(),B.end());
    if(sz1%2==0 && sz2%2==0 && sz3%2==0)
    {
        printf("0\n");
        return;
    }
    if(sz1%2==0)
    {
        printf("%lld\n",func(B,G,R));
    }
    else if(sz2%2==0)
    {
        printf("%lld\n",func(G,R,B));
    }
    else
    {
        printf("%lld\n",func(B,R,G));
    }
}
 
int main()
{
    int t=1;
    //scanf("%d",&t);
    while(t--)solve();
    return 0;
}
#include<bits/stdc++.h>
#define For(a,b,c) for(int a=b;a<=c;++a)
#define Dor(a,b,c) for(int a=b;a>=c;--a)
#define CC(_...) fprintf(stderr,_)
using namespace std;
typedef long long LL;
enum{N=400007};
int n,S[3];
LL ans;
struct O {
LL a; int c;
bool operator < (const O &_) const {
	return a<_.a;
}
}A[N];
int main() {
	scanf("%d",&n);
	For(i,1,n*2) {
		char c[9];
		scanf("%lld%s",&A[i].a,c);
		if(*c=='R') A[i].c=0;
		if(*c=='G') A[i].c=1;
		if(*c=='B') A[i].c=2;
	}
	For(i,1,n*2) ++S[A[i].c];
	LL a=(S[0]&1),b=(S[1]&1),c=(S[2]&1),p,q,ac,bc,ca,cb;
	if(!a&&!b&&!c) {
		puts("0");
		return 0;
	}
	if(a&&b) p=0,q=1;
	if(a&&c) p=0,q=2;
	if(b&&c) p=1,q=2;
	sort(A+1,A+1+n*2);
	a=b=c=-1LL<<59,ac=bc=ca=cb=ans=1LL<<59;
	For(i,1,n*2) {
		if(A[i].c==p) {
			ca=min(ca,A[i].a-c);
			ans=min(ans,A[i].a-b);
			a=A[i].a;
		}
		else if(A[i].c==q) {
			cb=min(cb,A[i].a-c);
			ans=min(ans,A[i].a-a);
			b=A[i].a;
		}
		else {
			ac=min(ac,A[i].a-a);
			bc=min(bc,A[i].a-b);
			c=A[i].a;
		}
	}
	ans=min(ans,min(ca,ac)+min(cb,bc));
	printf("%lld",ans);
	return 0;
}