#include<bits/stdc++.h>
using namespace std;
const int M = 1e9 + 7;
const int MAX = 1e5 + 5;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define int long long int
#define pb push_back
#define print(x) cout<<x<<"\n"
#define time_passed	1.0 * clock() / CLOCKS_PER_SEC

//basic mathematics
int gcd(int a, int b)
{
	if (a == 0)
		return b;
	return gcd(b % a, a);
}
int lcm(int a, int b)
{
	return (a * b) / gcd(a, b);
}

//modular arithmetic
int powermod(int x, int y, int p)
{
	int res = 1;

	x = x % p;

	if (x == 0) return 0;

	while (y > 0)
	{
		if (y & 1)
			res = (res * x) % p;
		y = y >> 1;
		x = (x * x) % p;
	}
	return res;
}

//SOURCE GEEKS FOR GEEKS
bool solve(string n, int l) 
{ 
  
    // Less than three digit number 
    // can be checked directly. 
    if (l < 3) { 
        if (stoi(n) % 8 == 0) 
            return true; 
  
        // check for the reverse of a number 
        reverse(n.begin(), n.end()); 
        if (stoi(n) % 8 == 0) 
            return true; 
        return false; 
    } 
  
    // Stores the Frequency of characters in the n. 
    int hash[10] = { 0 }; 
    for (int i = 0; i < l; i++) 
        hash[n[i] - '0']++; 
  
    // Iterates for all three digit numbers 
    // divisible by 8 
    for (int i = 104; i < 1000; i += 8) { 
  
        int dup = i; 
  
        // stores the frequency of all single 
        // digit in three-digit number 
        int freq[10] = { 0 }; 
        freq[dup % 10]++; 
        dup = dup / 10; 
        freq[dup % 10]++; 
        dup = dup / 10; 
        freq[dup % 10]++; 
  
        dup = i; 
  
        // check if the original number has 
        // the digit 
        if (freq[dup % 10] > hash[dup % 10]) 
            continue; 
        dup = dup / 10; 
  
        if (freq[dup % 10] > hash[dup % 10]) 
            continue; 
        dup = dup / 10; 
  
        if (freq[dup % 10] > hash[dup % 10]) 
            continue; 
  
        return true; 
    } 
  
    // when all are checked its not possible 
    return false; 
} 

void solve()
{
    string s;
    cin >> s;
    if (solve(s, s.size())) 
        cout << "Yes"; 
    else
        cout << "No";
	
}

int32_t main(void)
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	fastio
	int t = 1;
	//cin >> t;
	while (t--)
	{
		solve();
	}
	cerr << "\n" << "Time elapsed : " << time_passed << "\n";
	return 0;
}
#include <bits/stdc++.h>

typedef long double ld;
#define int long long

#define gcd            __gcd
#define endl           "\n"
#define setbits(x)     __builtin_popcountll(x)
#define zrobits(x)     __builtin_ctzll(x)
#define mod            1000000007
#define mod2           998244353
#define maxe           *max_element
#define mine           *min_element
#define inf            1e18
#define pb             push_back
#define all(x)         x.begin(), x.end()
#define f              first
#define s              second
#define lb             lower_bound
#define ub             upper_bound
#define ins            insert
#define sz(x)          (int)(x).size()
#define mk             make_pair
#define deci(x, y)     fixed<<setprecision(y)<<x
#define w(t)           int t; cin>>t; while(t--)
#define nitin          ios_base::sync_with_stdio(false); cin.tie(nullptr)
#define PI             3.141592653589793238
#define mem0(x)        memset(x,0,sizeof x)
#define mem1(x)        memset(x,-1,sizeof x)
using namespace std;
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.f << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }

void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }

#ifdef NITIN
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
vector<int> solve(vector<int>&a,vector<int>&b) {
    vector<int> temp;
    for (auto &c:a) {
        auto idx = lower_bound(all(b), c);
        int op1 = inf, op2 = inf;
        if (idx != b.end()) {
            op1 = min(op1, abs(*idx - c));
        }
        if (idx != b.begin()) {
            idx = prev(idx);
            op2 = min(op2, abs(*idx - c));
        }
        temp.push_back(min(op1, op2));
    }
    return temp;
}

void solve() {
    int n;
    cin>>n;
    vector<int>r,g,b;
    for(int i=0;i<2*n;i++) {
        int val;
        cin >> val;
        char col;
        cin >> col;
        if (col == 'R') {
            r.pb(val);
        } else if (col == 'G') {
            g.pb(val);
        } else {
            b.pb(val);
        }
    }
    sort(all(r));
    sort(all(g));
    sort(all(b));
    if(r.size()%2==0 && g.size()%2==0 && b.size()%2==0){
        cout<<0<<endl;
    }else{
        if(r.size()%2!=0) {
            if (b.size() % 2 == 0) {
                swap(r, b);
            } else {
                swap(r, g);
            }
        }
        int ans=inf;
        vector<int> temp=solve(g,b);
        ans=mine(all(temp));
        vector<int>fir=solve(r,g);
        vector<int>sec=solve(r,b);
        map<int,int>take;
        for(auto &c:sec)
            take[c]++;
        for(int i=0;i<r.size();i++) {
            take[sec[i]]--;
            if (take[sec[i]] == 0) {
                take.erase(sec[i]);
            }
            ans = min(ans, fir[i] + take.begin()->first);
            take[sec[i]]++;
        }
        cout<<ans<<endl;
    }
}

int32_t main() {
    nitin;

    solve();
}