#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <iomanip>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <complex>
#include <regex>

#define Debug cout << "line: " << __LINE__ << "Debug" << endl;
#define Yes cout << "Yes" << endl;
#define YES cout << "YES" << endl;
#define No cout << "No" << endl;
#define NO cout << "NO" << endl;
#define ALL(a) (a).begin(), (a).end()
#define MP make_pair
#define MOD 1000000007
#define PI 3.14159265358979323846

using namespace std;

using ll = long long;
using ld = long double;
using vi = vector<int>;
using vd = vector<double>;
using vl = vector<long long>;

void set_FPD(int n){
    cout << fixed << setprecision(n);
    return;
}

ll func(vl a,vl b,vl c){
    ll abmin=1e15,acmin=1e15,bcmin=1e15;
    for(ll &x:a){
        auto itr=lower_bound(ALL(b),x);
        abmin=min({abs(x-*itr),abs(x-*(itr-1)),abmin});
    }
    for(ll &x:a){
        auto itr=lower_bound(ALL(c),x);
        acmin=min({abs(x-*itr),abs(x-*(itr-1)),acmin});
    }
    for(ll &x:b){
        auto itr=lower_bound(ALL(c),x);
        bcmin=min({abs(x-*itr),abs(x-*(itr-1)),bcmin});
    }
    return min(abmin,acmin+bcmin);
}

ll func(vl a,vl b){
    ll abmin=1e15;
    for(ll &x:a){
        auto itr=lower_bound(ALL(b),x);
        abmin=min({abs(x-*itr),abs(x-*(itr-1)),abmin});
    }
    return abmin;
}

void Main(){
    int n;
    cin>>n;
    vl r,g,b;
    for(int i=0;i<2*n;i++){
        ll a;
        char c;
        cin>>a>>c;
        if(c=='R') r.push_back(a);
        else if(c=='G') g.push_back(a);
        else b.push_back(a);
    }
    ll rsize=r.size(),gsize=g.size(),bsize=b.size();
    if(rsize%2==0 && gsize%2==0 && bsize%2==0){
        cout<<0<<endl;
        return;
    }
    if(rsize!=0) sort(ALL(r));
    if(gsize!=0) sort(ALL(g));
    if(bsize!=0) sort(ALL(b));
    if(rsize%2==1 && gsize%2==1){
        if(bsize==0) cout<<func(r,g);
        else cout<<func(r,g,b);
    }
    else if(gsize%2==1 && bsize%2==1){
        if(rsize==0) cout<<func(g,b);
        else cout<<func(g,b,r);
    }
    else if(bsize%2==1 && rsize%2==1) {
        if(gsize==0) cout<<func(b,r);
        else cout<<func(b,r,g);
    }
    cout<<endl;
    return;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    Main();
    return 0;
}
#pragma region Macros
#include <bits/stdc++.h>
// #include <atcoder/all>

using std::cin;
using std::cout;
using std::endl;
using std::sort;
using std::string;
using std::vector;
// using namespace atcoder;
typedef long long ll;
#define int long long

#ifdef _DEBUG
#define DEBUG(x) cout << #x << ": " << x << endl;
#else
#define DEBUG(x)
#endif

#define ALL(obj) (obj).begin(), (obj).end()
#define BIT(n) (1LL << (n))
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define YesNo(bool)            \
    if (bool)                  \
    {                          \
        cout << "Yes" << endl; \
    }                          \
    else                       \
    {                          \
        cout << "No" << endl;  \
    }
#define PRINTARR(arr)     \
    for (auto e : arr)    \
        cout << e << " "; \
    cout << endl;

constexpr ll MOD = (ll)1e9 + 7;
constexpr ll INF = (ll)1e18;
constexpr double EPS = 1e-9;
const double PI = std::acos(-1);

#pragma endregion

#pragma region Library

#pragma endregion

int calc(vector<int> arr1, vector<int> arr2)
{
    if (arr1.size() == 0 || arr2.size() == 0)
    {
        return INF;
    }
    auto it1 = 0;
    auto it2 = 0;
    int ret = std::abs(arr1[0] - arr2[0]);
    while (!(it1 == (int)arr1.size() - 1 && it2 == (int)arr2.size() - 1))
    {
        ret = std::min(ret, std::abs(arr1[it1] - arr2[it2]));

        if (it1 == (int)arr1.size() - 1)
        {
            it2++;
        }
        else if (it2 == (int)arr2.size() - 1)
        {
            it1++;
        }
        else if (arr1[it1] < arr2[it2])
        {
            it1++;
        }
        else
        {
            it2++;
        }
    }

    ret = std::min(ret, std::abs(arr1[it1] - arr2[it2]));

    return ret;
}

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

    int n;
    cin >> n;

    vector<int> red, green, blue;
    REP(i, 2 * n)
    {
        int c;
        string color;
        cin >> c >> color;
        if (color == "R")
        {
            red.push_back(c);
        }
        if (color == "G")
        {
            green.push_back(c);
        }
        if (color == "B")
        {
            blue.push_back(c);
        }
    }

    if (red.size() % 2 == 0 && green.size() % 2 == 0 && blue.size() % 2 == 0)
    {
        cout << 0 << endl;
    }
    else
    {
        vector<int> even, odd1, odd2;
        if (red.size() % 2 == 0)
        {
            even = red;
            odd1 = green;
            odd2 = blue;
        }
        if (green.size() % 2 == 0)
        {
            even = green;
            odd1 = red;
            odd2 = blue;
        }
        if (blue.size() % 2 == 0)
        {
            even = blue;
            odd1 = green;
            odd2 = red;
        }

        std::sort(ALL(even));
        std::sort(ALL(odd1));
        std::sort(ALL(odd2));

        cout << std::min(calc(odd1, odd2), calc(odd1, even) + calc(even, odd2)) << endl;

        // cout << calc(odd1, even) << " " << calc(even, odd2) << endl;
        // PRINTARR(even);

        return 0;
    }
}
