#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<bitset>
#include<cmath>
#include<ctime>
#include<queue>
#include<map>
#include<set>

#define int long long
#define fi first
#define se second
#define max Max
#define min Min
#define abs Abs
#define lc (x<<1)
#define rc (x<<1|1)
#define mid ((l+r)>>1)
#define lowbit(x) (x&(-x))
#define fan(x) (((x-1)^1)+1)
#define mp(x,y) make_pair(x,y)
#define clr(f,n) memset(f,0,sizeof(int)*(n))
#define cpy(f,g,n) memcpy(f,g,sizeof(int)*(n))

using namespace std;

inline int read()
{
	int ans=0,f=1;
	char c=getchar();
	while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9'){ans=(ans<<1)+(ans<<3)+c-'0';c=getchar();}
	return ans*f;
}

inline void write(int x)
{
	if(x<0) putchar('-'),x=-x;
	if(x/10) write(x/10);
	putchar((char)(x%10)+'0');
}

template<typename T>inline T Abs(T a){return a>0?a:-a;};
template<typename T,typename TT>inline T Min(T a,TT b){return a<b?a:b;}
template<typename T,typename TT> inline T Max(T a,TT b){return a<b?b:a;}

const int N=2e5+5;
int n,A,B,C,a[N],b[N],c[N],p1[N],p2[N],pre[N],suc[N],ans=1e18;

signed main()
{
	n=read();
	for(int i=1;i<=n*2;++i)
	{
		int x=read();
		char opt[5];
		scanf("%s",opt+1);
		if(opt[1]=='R')
			a[++A]=x;
		else if(opt[1]=='G')
			b[++B]=x;
		else c[++C]=x;
	}
	if(A%2==0&&B%2==0&&C%2==0)
	{
		printf("0\n");
		return 0;
	}
	if(A%2==0&&C%2==1)
		swap(a,c),swap(A,C);
	if(B%2==0&&C%2==1)
		swap(b,c),swap(B,C);
	sort(a+1,a+1+A);
	sort(b+1,b+1+B);
	for(int i=1;i<=A;++i)
	{
		int pos=lower_bound(b+1,b+1+B,a[i])-b;
		if(pos>=1&&pos<=B) ans=min(ans,abs(a[i]-b[pos]));
		if(pos>=2&&pos<=B+1) ans=min(ans,abs(a[i]-b[pos-1]));
	}
	for(int i=1;i<=C;++i)
	{
		int pos=lower_bound(a+1,a+1+A,c[i])-a;p1[i]=1e18;
		if(pos>=1&&pos<=A) p1[i]=min(p1[i],abs(c[i]-a[pos]));
		if(pos>=2&&pos<=A+1) p1[i]=min(p1[i],abs(c[i]-a[pos-1]));
		
		pos=lower_bound(b+1,b+1+B,c[i])-b;p2[i]=1e18;
		if(pos>=1&&pos<=B) p2[i]=min(p2[i],abs(c[i]-b[pos]));
		if(pos>=2&&pos<=B+1) p2[i]=min(p2[i],abs(c[i]-b[pos-1]));
	}
	pre[0]=1e18;suc[C+1]=1e18;
	for(int i=1;i<=C;++i) pre[i]=min(pre[i-1],p2[i]);
	for(int i=C;i>=1;--i) suc[i]=min(suc[i+1],p2[i]);
	for(int i=1;i<=C;++i) ans=min(ans,p1[i]+min(pre[i-1],suc[i+1]));
	printf("%lld\n",ans);
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

#define ar array
#define ll long long

const int MAX_N = 1e5 + 1;
const ll MOD = 1e9 + 7;
const ll INF = 1e15;

void solve()
{
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int N;
    cin >> N;
    vector<ll> cutenesses;
    vector<string> colors;
    ll cute;
    string last_color = "U";
    string color;
    int red = 0, blue = 0, green = 0;
    bool rb = false, rg = false, bg = false;
    ll min_rb = INF, min_rg = INF, min_bg = INF;
    for (int t = 1; t <= 2 * N; t++)
    {
        cin >> cute;
        cin >> color;

        cutenesses.push_back(cute);
        colors.push_back(color);
        if (color.compare("R") == 0)
        {
            red += 1;
        }
        else if (color.compare("B") == 0)
        {
            blue += 1;
        }
        else
        {
            green += 1;
        }
        // cout << cute << " " << color << endl;
        // solve();
    }
    if ((red % 2 == 0) && (blue % 2 == 0) && (green % 2 == 0))
    {
        cout << 0;
    }
    else
    {

        vector<size_t> idx(cutenesses.size());
        iota(idx.begin(), idx.end(), 0);

        // sort indexes based on comparing values in v
        // using std::stable_sort instead of std::sort
        // to avoid unnecessary index re-orderings
        // when v contains elements of equal values
        stable_sort(idx.begin(), idx.end(),
                    [&cutenesses](size_t i1, size_t i2)
                    { return cutenesses[i1] < cutenesses[i2]; });
        for (int i = 0; i < 2 * N - 1; i++)
        {
            // cout << idx[i] << " " << idx[i + 1] << endl;
            // cout << colors[idx[i]] << " " << colors[idx[i + 1]] << endl;
            if (((colors[idx[i]].compare("R") == 0) && ((colors[idx[i + 1]].compare("B") == 0))) || (((colors[idx[i]].compare("B") == 0) && ((colors[idx[i + 1]].compare("R") == 0)))))
            {
                rb = true;
                min_rb = min(min_rb, abs(cutenesses[idx[i]] - cutenesses[idx[i + 1]]));
            }
            else if (((colors[idx[i]].compare("R") == 0) && ((colors[idx[i + 1]].compare("G") == 0))) || (((colors[idx[i]].compare("G") == 0) && ((colors[idx[i + 1]].compare("R") == 0)))))
            {
                rg = true;
                min_rg = min(min_rg, abs(cutenesses[idx[i]] - cutenesses[idx[i + 1]]));
            }
            else if (((colors[idx[i]].compare("G") == 0) && ((colors[idx[i + 1]].compare("B") == 0))) || (((colors[idx[i]].compare("B") == 0) && ((colors[idx[i + 1]].compare("G") == 0)))))
            {
                bg = true;
                min_bg = min(min_bg, abs(cutenesses[idx[i]] - cutenesses[idx[i + 1]]));
            }
        }
        // cout << red << " " << blue << " " << green << endl;
        // cout << rb << " " << rg << " " << bg << endl;
        // cout << min_rb << " " << min_rg << " " << min_bg << endl;
        if (red % 2 == 0)
        {
            cout << min(min_bg, min_rg + min_rb);
        }
        else if (blue % 2 == 0)
        {
            cout << min(min_rg, min_rb + min_bg);
        }
        else
        {
            cout << min(min_rb, min_rg + min_bg);
        }
    }
}