#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 <bits/stdc++.h>
using namespace std;
typedef long long int lld;
typedef pair<int,int> pi;
typedef pair<lld,lld> pl;
typedef pair<int,lld> pil;
typedef pair<lld,int> pli;
typedef vector<int> vit;
typedef vector<vit> vitt;
typedef vector<lld> vlt;
typedef vector<vlt> vltt;
typedef vector<pi> vpit;
typedef vector<vpit> vpitt;
typedef long double ld;
#define x first
#define y second
#define pb push_back
#define all(v) v.begin(), v.end()
#define sz(x) (int)x.size()
#define mk(a,b) make_pair(a,b)
bool isrange(int y,int x,int n,int m){
	 if(0<=y&&y<n&&0<=x&&x<m) return true;
	 return false;
}
int dy[4] = {1,0,-1,0},dx[4]={0,1,0,-1},ddy[8] = {1,0,-1,0,1,1,-1,-1},ddx[8] = {0,1,0,-1,1,-1,1,-1};

vector<pair<lld,int> > v1,v2;
vector<lld> v[3];
void solve(int tc){
	int n;
	cin >> n;
	for(int e=0;e<n*2;e++){
		lld x;
		char y[2];
		cin >> x >> y;
		if(y[0]=='B') v[0].push_back(x);
		else if(y[0]=='R') v[1].push_back(x);
		else v[2].push_back(x);
	}
	for(int e=0;e<3;e++) sort(all(v[e]));
	int f1 = -1,f2 = -1,f3 = -1;
	for(int e=0;e<3;e++){
		if(sz(v[e])%2){
			if(f1==-1) f1 = e;
			else if(f2==-1) f2 = e;
		}else{
			f3 = e;
		}
	}
	if(f1==-1){
		cout << "0";
	}else{
		lld ans1 = 1e18;
		for(int e=0;e<sz(v[f1]);e++){
			lld x = v[f1][e];
			int wh = lower_bound(all(v[f2]),x) - v[f2].begin();
			if(wh!=sz(v[f2])){
				ans1 = min(ans1,abs(v[f2][wh]-x));
			}
			if(wh){
				wh--;
				ans1 = min(ans1,abs(v[f2][wh]-x));
			}
		}
		for(int e=0;e<sz(v[f3]);e++){
			lld x = v[f3][e];
			int wh = lower_bound(all(v[f1]),x) - v[f1].begin();
			lld ans2 = 1e18;
			if(wh!=sz(v[f1])){
				ans2 = min(ans2,abs(v[f1][wh]-x));
			}
			if(wh){
				wh--;
				ans2 = min(ans2,abs(v[f1][wh]-x));
			}
			v1.push_back(mk(ans2,e));
		}
		for(int e=0;e<sz(v[f3]);e++){
			lld x= v[f3][e];
			int wh = lower_bound(all(v[f2]),x) - v[f2].begin();
			lld ans3 = 1e18;
			if(wh!=sz(v[f2])){
				ans3 = min(ans3,abs(v[f2][wh]-x));
			}
			if(wh){
				wh--;
				ans3 = min(ans3,abs(v[f2][wh]-x));
			}
			v2.push_back(mk(ans3,e));
		}
		sort(all(v1));
		sort(all(v2));
		lld ans2 = 1e18;
		for(int e=0;e<min(sz(v1),3);e++){
			for(int p=0;p<min(sz(v2),3);p++){
				if(v1[e].y!=v2[p].y){
					ans2 = min(ans2,v1[e].x+v2[p].x);
				}
			}
		}
		cout << min(ans1,ans2);
	}
}


int main(void){
	
	ios_base :: sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	
	int tc = 1;
	/*
		cin >> tc;
	*/
	for(int test_number=1;test_number<=tc;test_number++){
		solve(test_number);
	}
	return 0;
}