#include<cstdio>
#include<algorithm>
#define N 300010
#define db double
#define ll long long
#define fo(x,a,b) for(int x=(a);x<=(b);x++)
#define fd(x,a,b) for(int x=(a);x>=(b);x--)
using namespace std;
struct node{ll x; int id;}a[N];
int n,t[4],tot1=0,tot2=0,tot3=0,tot4=0;
ll b[N],c[N],d[N],e[N];
ll ans=(1ll<<50),ans1=(1ll<<50),ans2=(1ll<<50);

inline ll read() {
	ll x=0;int f=0;char c=getchar();
	while (c<'0'||c>'9') f=(c=='-')?1:f,c=getchar();
	while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
	return f?-x:x;
}

int gc() {
	char c=getchar(); while (c!='R'&&c!='G'&&c!='B') c=getchar();
	if (c=='R') return 1; else if (c=='G') return 2; else return 3;
}

inline bool cmp(node x,node y) {return x.x<y.x;}

void calc1() {
	ans1=ans2=(1ll<<50);
	int pos=-1;
	fo(i,1,tot1) {
		int l=1,r=tot2,mid;
		ll res=(1ll<<50);
		while (l<=r) {
			mid=(l+r)>>1; res=min(res,abs(c[mid]-b[i]));
			if (c[mid]>=b[i]) r=mid-1; else l=mid+1;
		}
		if (res<ans1) ans1=res,pos=i;
	}
	fo(i,1,tot1) {
		int l=1,r=tot3,mid;
		ll res=(1ll<<50);
		while (l<=r) {
			mid=(l+r)>>1; res=min(res,abs(d[mid]-b[i]));
			if (d[mid]>=b[i]) r=mid-1; else l=mid+1;
		}
		if (res<ans2&&i!=pos) ans2=res;
	}
	ans=min(ans,ans1+ans2);
}

void calc2() {
	ans1=ans2=(1ll<<50);
	int pos=-1;
	fo(i,1,tot1) {
		int l=1,r=tot3,mid;
		ll res=(1ll<<50);
		while (l<=r) {
			mid=(l+r)>>1; res=min(res,abs(d[mid]-b[i]));
			if (d[mid]>=b[i]) r=mid-1; else l=mid+1;
		}
		if (res<ans2) ans2=res,pos=i;
	}
	fo(i,1,tot1) {
		int l=1,r=tot2,mid;
		ll res=(1ll<<50);
		while (l<=r) {
			mid=(l+r)>>1; res=min(res,abs(c[mid]-b[i]));
			if (c[mid]>=b[i]) r=mid-1; else l=mid+1;
		}
		if (res<ans1&&i!=pos) ans1=res;
	}
	ans=min(ans,ans1+ans2);
}

int main() {
	n=read();n=2*n;
	fo(i,1,n) {
		a[i].x=read(),a[i].id=gc(),t[a[i].id]++;
		if (a[i].id==1) b[++tot1]=a[i].x;
		else if (a[i].id==2) c[++tot2]=a[i].x;
		else d[++tot3]=a[i].x;
	}
	sort(a+1,a+n+1,cmp);
	if (t[1]%2==0&&t[2]%2==0&&t[3]%2==0)
		return 0&printf("0\n");
	sort(b+1,b+tot1+1);
	sort(c+1,c+tot2+1);
	sort(d+1,d+tot3+1);
	if (tot2%2==0) {
		swap(b,c),swap(tot1,tot2);
	}
	else if (tot3%2==0) {
		swap(b,d),swap(tot1,tot3);
	}
	fo(i,1,tot2) {
		int l=1,r=tot3,mid;
		ll res=(1ll<<50);
		while (l<=r) {
			mid=(l+r)>>1; res=min(res,abs(d[mid]-c[i]));
			if (d[mid]>=c[i]) r=mid-1; else l=mid+1;
		}
		ans=min(ans,res);
	}
	calc1();
	calc2();
	printf("%lld\n",ans);
	return 0;
}

#include <iostream>
#include <vector>
#include <math.h>
#include <algorithm>

using namespace std;

// Aの中でvalに最も近い値を二分探索で探し、差の絶対値を返す
long long findClosestVal(vector<long long> *A, long long val)
{
    long long lb, ub;
    lb = 0;
    ub = A->size() - 1;
    while (lb + 1 < ub)
    {
        long long mid = (lb + ub) / 2;
        if ((*A)[mid] <= val)
        {
            lb = mid;
        }
        else
        {
            ub = mid;
        }
    }
    return min(abs(val - (*A)[lb]), abs(val - (*A)[ub]));
}

int main()
{
    int N;
    cin >> N;
    vector<long long> R, G, B;
    for (int i = 0; i < 2 * N; i++)
    {
        long long a;
        string c;
        cin >> a >> c;
        if (c == "R")
            R.push_back(a);
        if (c == "G")
            G.push_back(a);
        if (c == "B")
            B.push_back(a);
    }

    if (R.size() % 2 == 0 && G.size() % 2 == 0 && B.size() % 2 == 0)
    {
        cout << 0 << endl;
        return 0;
    }


    vector<long long> *pO1, *pO2, *pE;
    if (R.size() % 2 == 0)
    {
        pE = &R;
        pO1 = &G;
        pO2 = &B;
    }
    else if (G.size() % 2 == 0)
    {
        pE = &G;
        pO1 = &B;
        pO2 = &R;
    }
    else if (B.size() % 2 == 0)
    {
        pE = &B;
        pO1 = &R;
        pO2 = &G;
    }

    sort(pO1->begin(), pO1->end());
    sort(pO2->begin(), pO2->end());
    sort(pE->begin(), pE->end());

    long long ans = 1e15;
    for (vector<long long>::iterator iter = pO1->begin(); iter < pO1->end(); iter++)
    {
        long long tmp = findClosestVal(pO2, *iter);
        ans = min(ans, tmp);
    }
    // cout << "ans " << ans << endl;


    long long val1, val2;
    val1 = 1e15;
    val2 = 1e15;
    for (vector<long long>::iterator iter = pE->begin(); iter < pE->end(); iter++)
    {
        long long tmp1 = findClosestVal(pO1, *iter);
        long long tmp2 = findClosestVal(pO2, *iter);
        val1 = min(val1, tmp1);
        val2 = min(val2, tmp2);
    }
    ans = min(ans, val1 + val2);
    cout << ans << endl;
}