#include<bits/stdc++.h>
#include<iostream>
#include<string>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<iomanip>
#include<cstdlib>
#include<ctime>
#include<set>
#include<map>
#include<utility>
#include<queue>
#include<vector>
#include<stack>
#include<sstream>
#include<algorithm>
#define forn(i,a,b)for(int i=(a),_b=(b);i<=_b;i++)
#define fore(i,b,a)for(int i=(b),_a=(a);i>=_a;i--)
#define rep(i,n)for(int i=0,_n=n;i<n;i++)
#define ll long long
#define pii pair<int,int>
#define vi vector<int>
#define vpii vector<pii>
#define m_p make_pair
#define re return
#define pb push_back
#define si set<int>
#define ld long double
#define X first
#define Y second
#define st string
#define ull unsigned long long
#define mod 1000000007
#define INF 1000000007
#define x1 XZVJDFADSPFOE
#define y1 GASDIJSLDAEJF
#define x2 DFDAJKVOHKWIW
#define y2 PSFSAODSXVNMQ
#define LLINF 0x3f3f3f3f3f3f3f3fLL
#define foreach(i,c) for(__typeof(c.begin())i=(c.begin());i!=(c).end();i++)
using namespace std;
inline void read(int &x)
{
	short negative=1;
    x=0;
    char c=getchar();
    while(c<'0' || c>'9')
    {
		if(c=='-')
			negative=-1;
		c=getchar();
	}
    while(c>='0' && c<='9')
        x=(x<<3)+(x<<1)+(c^48),c=getchar();
    x*=negative;
}
ll quickpower(ll n,ll k){
	ll ans=1;
	while(k){
		if(k%2){
			ans*=n;
			ans%=mod;
		}
		n*=n;
		n%=mod;
		k/=2;
	}
	return ans;
}
string int_to_string(int n)
{
	string s="";
	while(n)
	{
		int now=n%10;
		s+=now+'0';
		n/=10;
	}
	reverse(s.begin(),s.end());
	return s;
}
int string_to_int(string s)
{
	int n=0;
	rep(i,s.size())
	{
		n*=10;
		n+=s[i]-'0';
	}
	return n;
}
int n;
int main()
{
	ios::sync_with_stdio(0);
	cin>>n;
	cout<<(1<<n)-1<<endl;
	forn(i,1,(1<<n)-1)
	{
		rep(j,1<<n)
		{
			if(__builtin_popcount(i&j)%2) cout<<'B';
			else cout<<'A';
		}
		cout<<endl;
	}
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template<class T> using vc = vector<T>;
template<class T> using vvc = vc<vc<T>>;
template<class T> using vvvc = vc<vvc<T>>;
template<class T> using vvvvc = vvc<vvc<T>>;
template<class T> using PQ = priority_queue<T>;
template<class T> using invPQ = priority_queue<T, vector<T>, greater<T>>;
using IP = pair<int, int>;
using LP = pair<ll, ll>;
#define all(x) begin(x), end(x)
#define rep(i, n) for (int i = 0; (i) < (int)(n); i++)
#define rep3(i, m, n) for (int i = (m); (i) < (int)(n); i++)
#define repr(i, n) for (int i = (n) - 1; (i) >= 0; i--)
#define rep3r(i, m, n) for (int i = (n) - 1; (i) >= (int)(m); i--)
template<class T> inline bool chmax(T & a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T & a, T b) { if (a > b) { a = b; return 1; } return 0; }
constexpr long long LINF = 4611686015206162431;

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int N;
    cin >> N;
    ll a[2*N];
    vc<int> c(2*N);
    vvc<ll> dogs(3);
    rep (i, N*2) {
        cin >> a[i];
        char col;
        cin >> col;
        if (col == 'G') c[i] = 1;
        else if (col == 'B') c[i] = 2;
        dogs[c[i]].emplace_back(a[i]);
    }
    rep (i, 3) {
        sort(all(dogs[i]));
    }

    int oddcnt = dogs[0].size() % 2 + dogs[1].size() % 2 + dogs[2].size() % 2;
    ll ans = LINF;
    if (oddcnt == 0) ans = 0;
    else {
        int even = 0;
        rep (i, 3) if (dogs[i].size() % 2 == 0) even = i;
        int odd1 = (even + 1) % 3, odd2 = (even + 2) % 3;

        rep (i, dogs[odd1].size()) {
            int pos = lower_bound(all(dogs[odd2]), dogs[odd1][i]) - dogs[odd2].begin();
            if (pos < dogs[odd2].size()) chmin(ans, abs(dogs[odd2][pos] - dogs[odd1][i]));
            if (pos > 0) {
                chmin(ans, abs(dogs[odd2][pos-1] - dogs[odd1][i]));
            }
        }

        ll tmp1 = LINF, tmp2 = LINF;
        rep (i, dogs[odd1].size()) {
            int pos1 = lower_bound(all(dogs[even]), dogs[odd1][i]) - dogs[even].begin();
            if (pos1 < dogs[even].size()) chmin(tmp1, abs(dogs[even][pos1] - dogs[odd1][i]));
            if (pos1 > 0) chmin(tmp1, abs(dogs[even][pos1-1] - dogs[odd1][i]));
        }
        rep (i, dogs[odd2].size()) {
            int pos2 = lower_bound(all(dogs[even]), dogs[odd2][i]) - dogs[even].begin();
            if (pos2 < dogs[even].size()) chmin(tmp2, abs(dogs[even][pos2] - dogs[odd2][i]));
            if (pos2 > 0) chmin(tmp2, abs(dogs[even][pos2-1] - dogs[odd2][i]));
        }
        chmin(ans, tmp1 + tmp2);
    }

    cout << ans << endl;
}