#include<bits/stdc++.h>
namespace my_std{
	using namespace std;
	#define pii pair<int,int>
	#define fir first
	#define sec second
	#define MP make_pair
	#define rep(i,x,y) for (int i=(x);i<=(y);i++)
	#define drep(i,x,y) for (int i=(x);i>=(y);i--)
	#define go(x) for (int i=head[x];i;i=edge[i].nxt)
	#define templ template<typename T>
	#define sz 202020
	typedef long long ll;
	typedef double db;
	mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
	templ inline T rnd(T l,T r) {return uniform_int_distribution<T>(l,r)(rng);}
	templ inline bool chkmax(T &x,T y){return x<y?x=y,1:0;}
	templ inline bool chkmin(T &x,T y){return x>y?x=y,1:0;}
	templ inline void read(T& t)
	{
		t=0;char f=0,ch=getchar();double d=0.1;
		while(ch>'9'||ch<'0') f|=(ch=='-'),ch=getchar();
		while(ch<='9'&&ch>='0') t=t*10+ch-48,ch=getchar();
		if(ch=='.'){ch=getchar();while(ch<='9'&&ch>='0') t+=d*(ch^48),d*=0.1,ch=getchar();}
		t=(f?-t:t);
	}
	template<typename T,typename... Args>inline void read(T& t,Args&... args){read(t); read(args...);}
	char __sr[1<<21],__z[20];int __C=-1,__zz=0;
	inline void Ot(){fwrite(__sr,1,__C+1,stdout),__C=-1;}
	inline void print(int x)
	{
		if(__C>1<<20)Ot();if(x<0)__sr[++__C]='-',x=-x;
		while(__z[++__zz]=x%10+48,x/=10);
		while(__sr[++__C]=__z[__zz],--__zz);__sr[++__C]='\n';
	}
	void file()
	{
		#ifdef NTFOrz
		freopen("a.in","r",stdin);
		#endif
	}
	inline void chktime()
	{
		#ifdef NTFOrz
		cerr<<clock()/1000.0<<'\n';
		#endif
	}
	#ifdef mod
	ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x%mod) if (y&1) ret=ret*x%mod;return ret;}
	ll inv(ll x){return ksm(x,mod-2);}
	#else
	ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x) if (y&1) ret=ret*x;return ret;}
	#endif
//	inline ll mul(ll a,ll b){ll d=(ll)(a*(double)b/mod+0.5);ll ret=a*b-d*mod;if (ret<0) ret+=mod;return ret;}
}
using namespace my_std;

int n;
vector<ll>a[3];

int main()
{
	file();
	map<char,int>mp; mp['R']=0,mp['G']=1,mp['B']=2;
	read(n);
	ll x; char cc;
	rep(i,1,n*2) read(x),cin>>cc,a[mp[cc]].push_back(x);
	rep(i,0,2) sort(a[i].begin(),a[i].end());
	auto qq=[&](int id,ll w)
	{
		if (!a[id].size()) return ll(1e18);
		int t=lower_bound(a[id].begin(),a[id].end(),w)-a[id].begin(); ll res=1e18;
		if (t) chkmin(res,w-a[id][t-1]);
		if (t!=(int)a[id].size()) chkmin(res,a[id][t]-w);
		return res;
	};
	int c=0,p=0; rep(i,0,2) if (a[i].size()%2==0u) ++c,p=i; if (c==3) return puts("0"),0;
	ll ans=1e18;
	rep(i,0,2) if (i!=p) for (auto x:a[i]) chkmin(ans,qq(3-i-p,x));
	int p1=(p==0?1:0),p2=3-p-p1;
	auto wk=[&](){ll mn=1e18;for (auto x:a[p]) chkmin(ans,mn+qq(p2,x)),chkmin(mn,qq(p1,x));};
	wk(); swap(p1,p2); wk();
	cout<<ans;
	return 0;
}
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
#define int long long
vector < int > G[3];
int n, pos_l, pos_r;

int id( char ch ) {
	if( ch == 'R' ) return 0;
	if( ch == 'B' ) return 1;
	if( ch == 'G' ) return 2;
}

int Fabs( int x ) {
	return x < 0 ? -x : x;
}

void work( int c, int val ) {
	int l = 0, r = G[c].size() - 1;
	pos_l = 0, pos_r = G[c].size() - 1;
	while( l <= r ) {
		int mid = ( l + r ) >> 1;
		if( G[c][mid] <= val ) pos_l = mid, l = mid + 1;
		else r = mid - 1;
	}
	l = 0, r = G[c].size() - 1;
	while( l <= r ) {
		int mid = ( l + r ) >> 1;
		if( G[c][mid] >= val ) pos_r = mid, r = mid - 1;
		else l = mid + 1;
	}
}

signed main() {
	scanf( "%lld", &n );
	for( int i = 1;i <= ( n << 1 );i ++ ) {
		char c; int a;
		scanf( "%lld %c", &a, &c );
		G[id( c )].push_back( a );
	}
	int c1 = -1, c2 = -1;
	for( int i = 0;i < 3;i ++ )
		if( G[i].size() & 1 ) {
			if( ~ c1 ) c2 = i;
			else c1 = i;
		}
	if( c1 == -1 ) return ! printf( "0\n" );
	else {
		int ans = 1e18;
		sort( G[c2].begin(), G[c2].end() );
		for( int i = 0;i < G[c1].size();i ++ ) {
			work( c2, G[c1][i] );
			ans = min( ans, min( Fabs( G[c1][i] - G[c2][pos_l] ), Fabs( G[c2][pos_r] - G[c1][i] ) ) );
		}
		int ans1 = 1e18, ans2 = 1e18, c;
		for( int i = 0;i < 3;i ++ )
			if( c1 == i || c2 == i ) continue;
			else c = i;
		if( ! G[c].size() ) goto pass;
		sort( G[c].begin(), G[c].end() );
		for( int i = 0;i < G[c1].size();i ++ ) {
			work( c, G[c1][i] );
			ans1 = min( ans1, min( Fabs( G[c1][i] - G[c][pos_l] ), Fabs( G[c][pos_r] - G[c1][i] ) ) );
		}
		for( int i = 0;i < G[c2].size();i ++ ) {
			work( c, G[c2][i] );
			ans2 = min( ans2, min( Fabs( G[c2][i] - G[c][pos_l] ), Fabs( G[c][pos_r] - G[c2][i] ) ) );
		}
		pass :
			printf( "%lld\n", min( ans, ans1 + ans2 ) );
	}
	return 0;
}