#include <bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

#define FS              ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll              long long int
#define ld              long double
#define pb              push_back
#define bp              __builtin_popcount
#define sz              size()
#define ff              first
#define ss              second
#define vll             vector<ll>
#define vbool           vector<bool>
#define vpll            vector<pair<ll,ll>>
#define pll             pair<ll,ll>
#define vllv            vector<vector<ll>>
#define setpri(x)       cout<<setprecision(x)<<fixed;
#define all(v)          v.begin(),v.end()
#define allr(v)         v.rbegin(),v.rend()
#define yesr {cout<<"YES"<<endl;return;}
#define nor {cout<<"NO"<<endl;return;}
// getline (std::cin,name);
ll MOD=1e9+7;
ll ceil1(ll n,ll x){return (n-1)/x+(n>0);}

ll gcd(ll a,ll b){return __gcd(a,b);}
ll lcm(ll a,ll b){return (max(a,b)/gcd(a,b))*min(a,b);}

ll pow1(ll n,ll m ,ll mod=MOD );
ll pow2(ll n,ll k);

ll modinv(ll n,ll mod=MOD){  return pow1(n,mod-2,mod);}

bool func(pair<ll,ll> &a,pair<ll,ll> &b ){
        if(a.ff != b.ff)return a.ff < b.ff;
        return a.ss > b.ss;
}

ll const N=(ll)2e5+11;
ll const LG=(ll)log2(N)+1;


// bool cmp(pll &a,pll &b){
//         if(a.ff!=b.ff)return a.ff<b.ff;
        
// }
void solve() {
	ll i,j,k,l,n,m,x,y,z,r;
        
        cin>>n;
        
        vllv a(n+11);
        for(i=0;i<n;i++){
                cin>>x>>y;
                a[y].pb(x);
        }
        vll an(n+11);
        a[0].pb(0);
        vll last(n+11);
        for(i=1;i<=n;i++){
                
                sort(all(a[i]));
                ll tt=0;
                for(j=1;j<a[i].sz;j++){
                        tt+=a[i][j]-a[i][j-1];
                }
                an[i]=tt;
                if(a[i].sz==0){
                        last[i]=last[i-1];
                }else{
                        last[i]=i;
                }
        }
        a[n+1].pb(0);
        vllv dp(n+11,vll(2));
        for(i=1;i<=n+1;i++){
                if(a[i].sz){
                        ll l=last[i-1];
                         m=a[l].sz;
                        k=a[i].sz;
                dp[i][0]=an[i] + min(dp[i-1][0]+abs(a[i][0]-a[l][m-1]),dp[i-1][1]+ abs(a[i][0]-a[l][0]));
                dp[i][1]=an[i] + min(dp[i-1][0]+abs(a[i][k-1]-a[l][m-1]),dp[i-1][1]+ abs(a[i][k-1]-a[l][0]));
                }else{
                        dp[i][0]=dp[i-1][0];
                        dp[i][1]=dp[i-1][1];
                }
                
        }
        // for(i=1;i<=n;i++){
        //         cout<<dp[i][0]<<" "<<dp[i][1]<<endl;
        // }
        m=a[n].sz;
        cout<<min(dp[n+1][1],dp[n+1][0]);
        
}
int main(){
        FS;

        ll i,j,k,n,m,x,y,z,q;
        q=1;
        // cin>>q;
        for(ll tc=1;tc<=q;tc++){
                // cout<<"Case #"<<tc<<": ";
                solve();
        }
        return 0;
}


ll pow1(ll n,ll m,ll mod){
        if(m==0)return 1;

        if(m%2==0)return pow1((n*n)%mod,m/2,mod);

        return (pow1((n*n)%mod,m/2,mod)*n)%mod;
}

ll pow2(ll n,ll k){
        ll ans=1;
            while(k>0){
                if(k%2==1)ans=ans*n;
                n=n*n;
                k/=2;
            }
            return ans;
}
#include<iostream>
#include<iomanip>
#include<string>
#include<vector>
#include<algorithm>
#include<utility>
#include<tuple>
#include<map>
#include<queue>
#include<deque>
#include<set>
#include<stack>
#include<numeric>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>

using namespace std;

struct Edge {
  int to;
  long long weight;
  Edge() : to(0), weight(0) {}
  Edge(int to, long long weight) : to(to), weight(weight) {}
  Edge(const Edge& e) {
    to = e.to;
    weight = e.weight;
  }
  bool operator>(const Edge &e) const { return weight > e.weight; }
  bool operator<(const Edge &e) const { return weight < e.weight; }
  bool operator==(const Edge &e) const { return weight == e.weight; }
  bool operator<=(const Edge &e) const { return weight <= e.weight; }
  bool operator>=(const Edge &e) const { return weight >= e.weight; }
};

using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using Graph = vector<vector<int>>;
using weightedGraph = vector<vector<Edge>>;
using heap = priority_queue<int, vector<int>, greater<int>>;

const ll BIL = 1e9;
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
const int inf = 1 << 29;
const ld PI = 3.141592653589793238462643383;

ll solve(vector<ll>& r, vector<ll>& g, vector<ll>& b) {
  ll res = INF;
  for(int i=0;i<g.size();i++) {
    auto it = lower_bound(b.begin(), b.end(), g[i]);
    if(it != b.end()) {
      res = min(res, abs(g[i]-*it));
    }
    if(it == b.begin()) continue;
    it--;
    res = min(res, abs(g[i]-*it));
  }
  for(int i=0;i<b.size();i++) {
    auto it = lower_bound(g.begin(), g.end(), b[i]);
    if(it != g.end()) {
      res = min(res, abs(b[i]-*it));
    }
    if(it == g.begin()) continue;
    it--;
    res = min(res, abs(b[i]-*it));
  }
  vector<pll> gr, br;
  for(int i=0;i<r.size();i++) {
    ll t = INF;
    auto it = lower_bound(g.begin(), g.end(), r[i]);
    if(it != g.end()) t = min(t, abs(r[i]-*it));
    if(it != g.begin()) {
      it--;
      t = min(t, abs(r[i]-*it));
    }
    gr.push_back({t, i});
    t = INF;
    auto it2 = lower_bound(b.begin(), b.end(), r[i]);
    if(it2 != b.end()) t = min(t, abs(r[i]-*it2));
    if(it2 != b.begin()) {
      it2--;
      t = min(t, abs(r[i]-*it2));
    }
    br.push_back({t, i});
  }
  sort(gr.begin(), gr.end());
  sort(br.begin(), br.end());
  ll res2 = INF;
  int i = 0;
  int j = 0;
  while(res2 == INF && i < gr.size() && j < br.size()) {
    if(gr[i].second != br[j].second) {
      res2 = gr[i].first + br[j].first;
      break;
    }
    if(i+1 == gr.size() && j+1 == br.size()) break;
    else if(i+1 == gr.size()) j++;
    else if(j+1 == br.size()) i++;
    else if(gr[i].first+br[j+1].first < gr[i+1].first+br[j].first) j++;
    else i++;
  }
  return min(res, res2);
}
int main(int argc,char* argv[]){
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout << fixed << setprecision(20);
  int n;
  cin >> n;
  int rn = 0, gn = 0, bn = 0;
  vector<ll> r, g, b;
  for(int i=0;i<2*n;i++) {
    ll a;
    char c;
    cin >> a >> c;
    if(c == 'R') {
      rn++;
      r.push_back(a);
    } else if(c == 'G') {
      gn++;
      g.push_back(a);
    } else {
      bn++;
      b.push_back(a);
    }
  }
  if(rn % 2 == 0 && gn % 2 == 0 && bn % 2 == 0) {
    cout << 0 << endl;
    return 0;
  }
  sort(r.begin(), r.end());
  sort(g.begin(), g.end());
  sort(b.begin(), b.end());
  ll ans = 0;
  if(rn % 2 == 0) {
    ans = solve(r, g, b);
  } else if(gn % 2 == 0) {
    ans = solve(g, r, b);
  } else {
    ans = solve(b, r, g);
  }
  cout << ans << endl;
  return 0;
}
