#include <algorithm>
#include <iostream>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); i++)

int main() {
    int n; std::cin >> n;
    std::vector<std::vector<int64_t> > a(3);
    std::string rgb = "RGB";
    REP(i, 2*n) {
        int64_t x; std::cin >> x;
        char c; std::cin >> c;
        a[rgb.find(c)].push_back(x);
    }
    std::vector<int> res;
    REP(i, 3) {
        std::sort(a[i].begin(), a[i].end());
        if (a[i].size() % 2)
            res.push_back(i);
    }
    if (res.empty()) {
        std::cout << 0 << '\n';
        return 0;
    }
    int u = res[0];
    int v = res[1];
    auto cal = [&](int u, int v) {
        int64_t ans = 1e18;
        if (a[v].empty())
            return ans;
        for (int i = 0, j = 0; i < static_cast<int>(a[u].size()); i++) {
            while (j+1 < static_cast<int>(a[v].size()) && a[v][j+1] <= a[u][i])
                j++;
            ans = std::min(ans, std::abs(a[u][i] - a[v][j]));
            if (j+1 < static_cast<int>(a[v].size()))
                ans = std::min(ans, std::abs(a[u][i] - a[v][j+1]));
        }
        return ans;
    };
    int w = 3 - u - v;
    std::cout << std::min(cal(u, v), cal(u, w) + cal(v, w)) << '\n';
    return 0;
}
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int BUF = 200005;
const long long INF = 1LL << 60;


int N;
pair<long long, char> vCh[BUF];

void read() {
    cin >> N;
    N *= 2;
    for (int i = 0; i < N; ++i) {
        cin >> vCh[i].first >> vCh[i].second;
    }
}


void work() {
    vector<long long> v[3];
    
    for (int i = 0; i < N; ++i) {
        static string s = "RGB";
        v[s.find(vCh[i].second)].push_back(vCh[i].first);
    }

    for (int i = 0; i < 3; ++i) {
        sort(v[i].begin(), v[i].end());
    }

    vector<int> colors;
    for (int i = 0; i < 3; ++i) {
        if (v[i].size() % 2 != 0) {
            colors.push_back(i);
        }
    }

    if (colors.empty()) {
        cout << 0 << endl;
        return;
    }

    for (int i = 0; i < 3; ++i) {
        if (v[i].size() % 2 == 0) {
            colors.push_back(i);
        }
    }
    
    long long minV = INF;
    for (int i = 0; i < v[colors[0]].size(); ++i) {
        vector<long long> &vList = v[colors[1]];
        int idx = upper_bound(vList.begin(), vList.end(), v[colors[0]][i]) - vList.begin();

        if (idx < vList.size()) {
            minV = min(minV, llabs(vList[idx] - v[colors[0]][i]));
        }

        if (0 <= idx - 1) {
            minV = min(minV, llabs(vList[idx - 1] - v[colors[0]][i]));
        }
    }

    vector<pair<long long, int> > costId[2];
    
    for (int i = 0; i < v[colors[2]].size(); ++i) {
        for (int j = 0; j < 2; ++j) {
            vector<long long> &vList = v[colors[j]];
            int idx = upper_bound(vList.begin(), vList.end(), v[colors[2]][i]) - vList.begin();

            long long toPush = INF;
            if (idx < vList.size()) {
                toPush = min(toPush, llabs(vList[idx] - v[colors[2]][i]));
            }

            if (0 <= idx - 1) {
                toPush = min(toPush, llabs(vList[idx - 1] - v[colors[2]][i]));
            }
            costId[j].push_back(make_pair(toPush, i));
        }
    }
    
    for (int i = 0; i < 2; ++i) {
        sort(costId[i].begin(), costId[i].end());
    }

    for (int i = 0; i < min((int)costId[0].size(), 2); ++i) {
        for (int j = 0; j < min((int)costId[1].size(), 2); ++j) {
            if (costId[0][i].second != costId[1][j].second) {
                minV = min(minV, costId[0][i].first + costId[1][j].first);
            }
        }
    }

    cout << minV << endl;
}


int main() {
    read();
    work();
    return 0;
}
