#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define mo 1000000007
#define ll long long
#define maxn 1005
using namespace std;

int n,i,j,k,f[2][2];
ll ans;

ll ksm(ll x,ll y){
	ll s=1;
	for(;y;y/=2,x=x*x%mo) if (y&1)
		s=s*x%mo;
	return s;
}

void reverse(){
	static int g[2][2];
	for(i=0;i<2;i++) for(j=0;j<2;j++)
		g[i^1][j^1]=f[j][i]^1;
	memcpy(f,g,sizeof(f));
}

void doit1(){
	static int f[maxn][2];
	f[1][0]=1;
	for(i=2;i<=n-1;i++){
		f[i][0]=(f[i-1][0]+f[i-1][1])%mo;
		f[i][1]=f[i-1][0];
	}
	printf("%d\n",f[n-1][0]);
}

int main(){
	scanf("%d",&n); char ch=getchar();
	for(i=0;i<2;i++) for(j=0;j<2;j++){
		while (ch!='A'&&ch!='B') ch=getchar();
		f[i][j]=ch-'A',ch=getchar();
	}
	if (f[0][1]==1) reverse();
	if (n==2||f[0][0]==0) printf("1"); else 
	if (f[1][0]==0) doit1(); else 
		printf("%lld\n",ksm(2,n-3));
}
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back
#define eb emplace_back
using namespace std;
template <class T = int>
using V = vector<T>;
template <class T = int>
using VV = V<V<T>>;
using ll = long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using pdd = pair<ld,ld>;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    int n2 = 2*n;
    V<ll> a(n2);
    V<char> c(n2);
    rep(i,n2) cin >> a[i] >> c[i];
    V<int> sum(3);
    rep(i,n2){
        if(c[i]=='R') sum[0]++;
        if(c[i]=='G') sum[1]++;
        if(c[i]=='B') sum[2]++;
    }
    if(sum[0]%2==0 && sum[1]%2==0 && sum[2]%2==0){
        cout << 0 << endl;
        return 0;
    }
    VV<ll> x(3);
    rep(i,n2){
        if(c[i]=='R') x[0].pb(a[i]);
        if(c[i]=='G') x[1].pb(a[i]);
        if(c[i]=='B') x[2].pb(a[i]);
    }
    rep(i,3) sort(all(x[i]));
    if(sum[0]%2==0) swap(x[0], x[2]);
    if(sum[1]%2==0) swap(x[1], x[2]);
    ll ans = 1e18;
    rep(i,x[0].size()){
        auto it = lower_bound(all(x[1]), x[0][i]);
        ans = min(ans, abs(*it-x[0][i]));
        if(it != x[1].begin()){
            it--;
            ans = min(ans, abs(*it-x[0][i]));
        }
    }
    rep(i,x[1].size()){
        auto it = lower_bound(all(x[0]), x[1][i]);
        ans = min(ans, abs(*it-x[1][i]));
        if(it != x[0].begin()){
            it--;
            ans = min(ans, abs(*it-x[1][i]));
        }
    }
    //cout << 1 << endl;
    if(x[2].size()==0){
        cout << ans << endl;
        return 0;
    }
    V<pll> t0, t1;
    rep(i,x[2].size()){
        auto it = lower_bound(all(x[0]), x[2][i]);
        if(it != x[0].end()) t0.eb(abs(*it-x[2][i]),i);
        if(it != x[0].begin()){
            it--;
            t0.eb(abs(*it-x[2][i]),i);
        }
    }
    rep(i,x[2].size()){
        auto it = lower_bound(all(x[1]), x[2][i]);
        if(it != x[1].end()) t1.eb(abs(*it-x[2][i]),i);
        if(it != x[1].begin()){
            it--;
            t1.eb(abs(*it-x[2][i]),i);
        }
    }
    sort(all(t0));
    sort(all(t1));
    //cout << 1 << endl;
    ll tmp = t0[0].first;
    rep(j,(int)t0.size()){
        rep(i,t1.size()){
            if(t1[i].second != t0[j].second){
                ans = min(ans, t0[j].first+t1[i].first);
                break;
            }
        }
    }
    //cout << 1 << endl;
    tmp = t1[0].first;
    rep(j,(int)t1.size()){
        rep(i,t0.size()){
            if(t0[i].second != t1[j].second){
                ans = min(ans, t1[j].first+t0[i].first);
                break;
            }
        }
    }
    cout << ans << endl;
}