#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (n); ++i)
using ll = long long;
const ll inf = 1e18;

ll solve() {
    int n; cin >> n;
    vector<vector<ll>> r(3);
    rep(i, 2 * n) {
        ll a; char c;
        cin >> a >> c;
        r[string("RGB").find(c)].push_back(a);
    }

    rep(i, 3) {
        r[i].push_back(-inf);
        r[i].push_back(inf);
        sort(r[i].begin(), r[i].end());
    }

    auto f = [&](int i, int j) -> ll {
        ll res = inf;
        for(auto &x : r[i]) {
            if(abs(x) == inf) continue;
            auto it = upper_bound(r[j].begin(), r[j].end(), x);
            res = min(res, min(*it - x, x - *prev(it)));
        }
        return res;
    };

    rep(i, 3) {
        int j = (i + 1) % 3;
        int k = (i + 2) % 3;
        if(r[j].size() % 2 && r[k].size() % 2) return min(f(j, k), f(i, j) + f(i, k));
    }

    return 0;
}

int main() {
    cout << solve() << '\n';
    return 0;
}

#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;
}
