//#pragma GCC optimize("Ofast")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("avx,avx2,fma")
#include <string>
#include <bits/functexcept.h>
#include <iosfwd>
#include <bits/cxxabi_forced.h>
#include <bits/functional_hash.h>

#pragma push_macro("__SIZEOF_LONG__")
#pragma push_macro("__cplusplus")
#define __SIZEOF_LONG__ __SIZEOF_LONG_LONG__
#define unsigned unsigned long
#define __cplusplus 201102L

#define __builtin_popcountl __builtin_popcountll
#define __builtin_ctzl __builtin_ctzll

#include <bitset>

#pragma pop_macro("__cplusplus")
#pragma pop_macro("__SIZEOF_LONG__")
#undef unsigned
#undef __builtin_popcountl
#undef __builtin_ctzl
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define mod 1000000007
#define fi first
#define se second
#define int long long
#define pii pair<int,int>
#define endl '\n'
#define pll pair<long long,long long>
#define LONGLONG_MAX 100000000000000
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
 
long long power(long long a,long long b){
    long long ans=1;
    while(b>0){
        if(b&1){ans=(ans*a)%mod;}
                a=(a*a)%mod;
                b>>=1;
        }
    return ans; 
}

int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int n;
    cin>>n;
    vector<int>v[3];
    for(int i=0;i<2*n;i++){
        int x;
        cin>>x;
        char c;
        cin>>c;
        if(c=='R')v[0].push_back(x);
        else if(c=='G')v[1].push_back(x);
        else v[2].push_back(x);
    }
    if(v[0].size()%2==0&&v[1].size()%2==0){
        cout<<0;
        return 0;
    }
    if(v[0].size()%2==1){
        //cout<<1<<" ";
        if(v[1].size()%2==0)swap(v[0],v[1]);
        else swap(v[0],v[2]);
        //cout<<v[0].size()<<" ";
    }
    int ans=LLONG_MAX;
    sort(v[0].begin(),v[0].end());
    sort(v[1].begin(),v[1].end());
    sort(v[2].begin(),v[2].end());
    //cout<<v[2].size()<<" "<<v[1].size()<<endl;
    for(auto i:v[1]){
        auto j=lower_bound(v[2].begin(),v[2].end(),i)-v[2].begin();
        if(j!=v[2].size())ans=min(ans,abs(i-v[2][j]));
        if(j>0){
            ans=min(ans,abs(i-v[2][j-1]));
        }
        
    }
    int a[n];
    multiset<int>s;
    for(int i=0;i<v[0].size();i++){
        int x=LLONG_MAX;
        auto j=lower_bound(v[2].begin(),v[2].end(),v[0][i])-v[2].begin();
        if(j!=v[2].size())x=min(x,abs(v[0][i]-v[2][j]));
        if(j>0){
            x=min(x,abs(v[0][i]-v[2][j-1]));
        }
        s.insert(x);
    }
    for(int i=0;i<v[0].size();i++){
        int x=LLONG_MAX;
        auto j=lower_bound(v[1].begin(),v[1].end(),v[0][i])-v[1].begin();
        if(j!=v[1].size())x=min(x,abs(v[0][i]-v[1][j]));
        if(j>0){
            x=min(x,abs(v[0][i]-v[1][j-1]));
        }
        int y=LLONG_MAX;
        j=lower_bound(v[2].begin(),v[2].end(),v[0][i])-v[2].begin();
        if(j!=v[2].size())y=min(y,abs(v[0][i]-v[2][j]));
        if(j>0){
            y=min(y,abs(v[0][i]-v[2][j-1]));
        }
        auto it=s.begin();
        if(*it==y)it++;
        ans=min(ans,x+*it);
    }
    cout<<ans;
    return 0;
}
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <utility>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <cstdio>
#include <limits>
#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
using namespace std;
template<class T>bool chmax(T &a, const T &b) { if(a < b){ a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if(a > b){ a = b; return 1; } return 0; }
template<class T> inline int  sz(T &a) { return a.size(); }
using ll = long long; using ld = long double;
using pi = pair<int,int>; using pl = pair<ll,ll>;
using vi = vector<int>; using vvi = vector<vi>;
using vl = vector<ll>; using vvl = vector<vl>;
const int inf = numeric_limits<int>::max();
const ll infll = numeric_limits<ll>::max();
int main()
{
  int n; cin >> n;
  vl a(2*n);
  vector<char> c(2*n);
  int Rcnt = 0, Bcnt = 0, Gcnt = 0;
  vl r,b,g;
  rep(i,2*n) {
    cin >> a[i] >> c[i];
    if(c[i] == 'R') {
      r.push_back(a[i]);
      Rcnt++;
    }
    if(c[i] == 'B') {
      b.push_back(a[i]);
      Bcnt++;
    }
    if(c[i] == 'G') {
      g.push_back(a[i]);      
      Gcnt++;
    }
  }

  if(Rcnt % 2 == 0 && Bcnt % 2 == 0 && Gcnt % 2 == 0) cout << 0 << "\n";
  else {
    // RB
    if(Rcnt % 2 == 1 && Bcnt % 2 == 1) {
    }
    else if(Rcnt % 2 == 1 && Gcnt % 2 == 1) {
      swap(g, b);
    }
    else {
      swap(g, r);
    }
      ll res = infll;
      sort(b.begin(),b.end());
      rep(i,sz(r)) {
        int lb = -1, ub = sz(b);
        while(ub - lb > 1) {
          int mid = (ub + lb) / 2;
          (b[mid] >= r[i] ? ub : lb) = mid;
        }
        if(ub != sz(b)) chmin(res, abs(r[i] - b[ub]));
        if(ub != 0) chmin(res, abs(r[i] - b[ub-1]));
      }
      sort(g.begin(),g.end());
      ll tmp1 = infll/2;
      ll tmp2 = infll/2;
      rep(i,sz(r)) {
        int lb = -1, ub = sz(g);
        while(ub - lb > 1) {
          int mid = (ub + lb) / 2;
          (g[mid] >= r[i] ? ub : lb) = mid;
        }
        if(ub != sz(g)) chmin(tmp1, abs(r[i] - g[ub]));
        if(ub != 0) chmin(tmp1, abs(r[i] - g[ub-1]));
      }
      rep(i,sz(b)) {
        int lb = -1, ub = sz(g);
        while(ub - lb > 1) {
          int mid = (ub + lb) / 2;
          (g[mid] >= b[i] ? ub : lb) = mid;
        }
        if(ub != sz(g)) chmin(tmp2, abs(b[i] - g[ub]));
        if(ub != 0) chmin(tmp2, abs(b[i] - g[ub-1]));
      }
      chmin(res, tmp1+tmp2);
      cout << res << "\n";
    
  }


  return 0;

}
