#define _USE_MATH_DEFIMES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

const int MOD = 1'000'000'007;
const int MOD2 = 998'244'353;
const int INF = 1'000'000'000; //1e9
const int NIL = -1;
const long long LINF = 1'000'000'000'000'000'000; // 1e18
const long double EPS = 1E-10;

template<class T, class S> inline bool chmax(T &a, const S &b){
    if(a < b){
        a = b; return true;
    }
    return false;
}
template<class T, class S> inline bool chmin(T &a, const S &b){
    if(b < a){
        a = b; return true;
    }
    return false;
}










int main(){
    int N; std::cin >> N;
    std::vector<std::vector<long long>> ct(3);
    {
        long long a; char c;
        std::vector<int> aa(26);
        aa['R' - 'A'] = 0;
        aa['G' - 'A'] = 1;
        aa['B' - 'A'] = 2;
        for(int i{0}, i_len{2 * N}; i < i_len; ++i){
            std::cin >> a >> c;
            ct[aa[c - 'A']].push_back(a);
        }
    }
    std::vector<int> E, Z, O;
    for(int i{0}; i < 3; ++i){
        std::sort(std::begin(ct[i]), std::end(ct[i]));
        if(!(ct[i].size() % 2)) E.push_back(i);
        if(ct[i].size() % 2) O.push_back(i);
        if(!ct[i].size()) Z.push_back(i);
    }
    if(E.size() == 3){
        std::cout << 0 << std::endl;
        return 0;
    }
    
    
    long long ans{LINF};
    {
        int a{O[0]}, b{O[1]};
        for(int i{0}, i_len{int(ct[a].size())}; i < i_len; ++i){
            auto itr{std::lower_bound(std::begin(ct[b]), std::end(ct[b]), ct[a][i])};
            if(itr != ct[b].end()) chmin(ans, std::abs(*itr - ct[a][i]));
            itr = std::upper_bound(std::begin(ct[b]), std::end(ct[b]), ct[a][i]);
            if(itr != ct[b].begin()){
                --itr;
                chmin(ans, std::abs(*itr - ct[a][i]));
            }
        }
    }

    std::vector<std::vector<std::pair<long long, int>>> z(2);
    if(!Z.size()){
        int e{E[0]};
        for(int i{0}, i_len{int(ct[e].size())}; i < i_len; ++i){
            for(int j{0}; j < 2; ++j){
                int o{O[j]};
                auto itr{std::lower_bound(std::begin(ct[o]), std::end(ct[o]), ct[e][i])};
                if(itr != ct[o].end()) z[j].emplace_back(std::abs(*itr - ct[e][i]), i);
                itr = std::upper_bound(std::begin(ct[o]), std::end(ct[o]), ct[e][i]);
                if(itr != ct[o].begin()){
                    --itr;
                    z[j].emplace_back(std::abs(*itr - ct[e][i]), i);
                }
            }
        }
    }

    for(int i{0}; i < 2; ++i){
        std::sort(std::begin(z[i]), std::end(z[i]));
        z[i].erase(std::unique(std::begin(z[i]), std::end(z[i])), std::end(z[i]));
    }

    for(int i{0}, i_len{std::min(4, int(z[0].size()))}; i < i_len; ++i){
        for(int j{0}, j_len{std::min(4, int(z[1].size()))}; j < j_len; ++j){
            if(z[0][i].second != z[1][j].second){
                chmin(ans, z[0][i].first + z[1][j].first);
            }
        }
    }
    std::cout << ans << std::endl;
    return 0;
}

#include <bits/stdc++.h>
#define rep(i, a, b) for(int i = (int)(a); i <= (int)(b); i++)

struct ad{
    int left, up, right, down;
};

int time_start;
int Num;
std::vector<ad> ads;
std::vector<std::pair<ad,int>> ads_start;

void in(){
    std::cin >> Num;
    ads.resize(Num);
    ads_start.resize(Num);
    rep(i,0,Num-1){
        std:: cin >> ads[i].left >> ads[i].up;
        ads[i].right = ads[i].left + 1;
        ads[i].down = ads[i].up + 1;
        ads_start[i].first = ads[i];
        std::cin >> ads_start[i].second;
    }
}

void make_randomcase(){
    
}

bool is_collision(int N){
    if(ads[N].left<0||ads[N].up<0||ads[N].right>10000||ads[N].down>10000) return true;
    rep(i,0,Num-1){
        if(i==N) continue;
        if( std::max(ads[i].left,ads[N].left) < std::min(ads[i].right,ads[N].right) && std::max(ads[i].up,ads[N].up) < std::min(ads[i].down,ads[N].down) ){
            return true;
        }
    }
    return false;
}

int score(){
    long double point=0;
    rep(i,0,Num-1){
        point+=1.0-pow(1-1.0*std::min(ads_start[i].second,(ads[i].right-ads[i].left)*(ads[i].down-ads[i].up))/std::max(ads_start[i].second,(ads[i].right-ads[i].left)*(ads[i].down-ads[i].up)),2);
    }
    point *= 1e9;
    point /= Num;
    return round(point);
}

//ランダムに広げる（広げて得をして、なおかつできるものだけ広げる）

void solve(){
    std::vector<std::vector<int>> delta(4,std::vector<int> (4,0));
    delta[0][0] = delta[1][1] = -1;
    delta[2][2] = delta[3][3] = 1;
    std::vector<ad> best_ads(Num);
    int bestscore = score();
    rep(_,0,44){
        rep(i,0,Num-1){
            ads[i] = ads_start[i].first;
        }
        time_start = clock();
        while(clock() - time_start < 100000){
            int select = random()%Num;
            int direction = random()%4;
            int stretch = ( 10 - (clock() - time_start) / 10000 ) * ( random()%10 + 1 );
            ads[select].left += stretch * delta[direction][0];
            ads[select].up += stretch * delta[direction][1];
            ads[select].right += stretch * delta[direction][2];
            ads[select].down += stretch * delta[direction][3];
            if(is_collision(select)||(ads[select].right-ads[select].left)*(ads[select].down-ads[select].up)>ads_start[select].second*std::min((long)10, 7 + ( clock() - time_start ) / 20000) / 10){
                ads[select].left -= stretch * delta[direction][0];
                ads[select].up -= stretch * delta[direction][1];
                ads[select].right -= stretch * delta[direction][2];
                ads[select].down -= stretch * delta[direction][3];
            }
        }
        if(bestscore < score()){
            bestscore = score();
            best_ads = ads;
        }
    }
    ads = best_ads;
}

void out(){
    rep(i,0,Num-1){
        std::cout << ads[i].left << ' ' << ads[i].up << ' ' << ads[i].right << ' ' << ads[i].down << '\n';
    }
}

int main() {
    srand((unsigned int)time(NULL));
    time_start = std::clock();
    in();
    solve();
    out();
    std::cerr << score() << std::endl;
}