/* Problem URL: https://codeforces.com/contest/962/problem/E */

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template <class T, class comp = less<>>
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;

#define V vector

#define rmin(a, b) a = min(a, b)
#define rmax(a, b) a = max(a, b)

#define rep(i, lim) for (int i = 0; i < (lim); i++)
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)

#define repv(i, v) for (auto &i : (v))
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
#define sortv(v) sort(v.begin(), v.end())
#define all(v) (v).begin(), (v).end()

using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vvvvi = vector<vvvi>;

using ll = long long;

using vl = vector<ll>;
using vvl = vector<vl>;
using vvvl = vector<vvl>;
using vvvvl = vector<vvvl>;

template<class v>
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
    os << vec[0];
    for (size_t i = 1; i < vec.size(); i++) {
        os << ' ' << vec[i];
    }
    os << '\n';
    return os;
}

template<class v>
auto operator>>(istream &is, vector<v> &vec)->istream& {
    for (auto &i : vec) {
        is >> i;
    }
    return is;
}

template<class v>
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
    for (auto &i : vec) {
        os << i[0];
        for (size_t j = 1; j < i.size(); j++) {
            os << ' ' << i[j];
        }
        os << '\n';
    }
    return os;
}

template<class v>
auto operator>>(istream  &is, vector<vector<v>> &vec)->istream& {
    for (auto &i : vec) {
        for (auto &j : i) {
            is >> j;
        }
    }
    return is;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;

    vl r;
    vl b;
    vl p;

    rep(i, n) {
        ll x;
        char o;
        cin >> x >> o;

        if (o == 'R') {
            r.push_back(x);
            continue;
        }

        if (o == 'B') {
            b.push_back(x);
            continue;
        }

        p.push_back(x);
    }

    sortv(r);
    sortv(b);
    sortv(p);

    ll ans = 0;
    nrep(i, 1, p.size()) {
        ans += p[i] - p[i - 1];
    }

    V<pair<ll, int>> tmp;

    repv(i, r) {
        tmp.emplace_back(i, 0);
    }
    repv(i, p) {
        tmp.emplace_back(i, 1);
    }
    sortv(tmp);

    nrep(i, 1, tmp.size()) {
        if (tmp[i].second == 0 || tmp[i - 1].second == 0) {
            ans += tmp[i].first - tmp[i - 1].first;
        }
    }

    tmp.clear();

    repv(i, b) {
        tmp.emplace_back(i, 0);
    }
    repv(i, p) {
        tmp.emplace_back(i, 1);
    }
    sortv(tmp);

    nrep(i, 1, tmp.size()) {
        if (tmp[i].second == 0 || tmp[i - 1].second == 0) {
            ans += tmp[i].first - tmp[i - 1].first;
        }
    }

    repv(i, r) {
        tmp.emplace_back(i, 2);
    }
    
    sortv(tmp);

    int prev = -1;
    bool ry = false;
    bool by = false;
    rep(i, tmp.size()) {
        if (tmp[i].second == 1) {
            if (prev == -1 || (!ry && !by)) {
                prev = i;
                ry = false;
                by = false;
                continue;
            }

            if (ry && by) {
                int prevr = -1;
                int prevb = -1;
                ll maxr = 0;
                ll maxb = 0;

                nrep(j, prev + 1, i) {
                    if (tmp[j].second == 2) {
                        if (prevr == -1) {
                            rmax(maxr, tmp[j].first - tmp[prev].first);
                        } else {
                            rmax(maxr, tmp[j].first - tmp[prevr].first);
                        }
                        prevr = j;
                        continue;
                    }

                    if (prevb == -1) {
                        rmax(maxb, tmp[j].first - tmp[prev].first);
                    } else {
                        rmax(maxb, tmp[j].first - tmp[prevb].first);
                    }
                    prevb = j;
                }

                rmax(maxr, tmp[i].first - tmp[prevr].first);
                rmax(maxb, tmp[i].first - tmp[prevb].first);

                // cout << maxr << ' ' << maxb << ' ' << tmp[i].first - tmp[prev].first << '\n';
                ans -= max(maxr + maxb, tmp[i].first - tmp[prev].first);

                prev = i;
                ry = false;
                by = false;
                continue;
            }

            ll maximal = 0;
            nrep(j, prev + 1, i + 1) {
                if (i > 0) {
                    rmax(maximal, tmp[j].first - tmp[j - 1].first);
                }
            }

            ans -= maximal;
            prev = i;
            ry = false;
            by = false;
            continue;
        }

        if (tmp[i].second == 2) {
            ry = true;
            continue;
        }

        by = true;
    }

    cout << ans << '\n';
}
