#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<stdio.h>
#include<deque>
#include<map>
#include<queue>
#include<cmath>
#include<set>

typedef long long ll;

#define debug(x) cout << #x << '=' << x << endl;
#define debug_arr(a, n) for(ll i = 0; i < n; i++){cout << a[i] << ' ';} cout << endl;
#define inf 100000000000
#define loop(i,n) for(ll i = 0; i < n; i++)
#define graph vector<vector<ll>>
#define P pair<ll,ll>

using namespace std;

int main(){
    ll n;
    cin >> n;
    vector<P> jobs(n);

    loop(i,n){
        cin >> jobs[i].first >> jobs[i].second;
    }

    ll ans = inf;
    loop(i,n){
        loop(j,n){
            if(i == j) ans = min(jobs[i].first + jobs[j].second, ans);
            else ans = min(max(jobs[i].first, jobs[j].second), ans);
        }
    }

    cout << ans << endl;
    return 0;
    
}
#include <bits/stdc++.h>
#define file(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)

using ll = long long;
const int N(1e5 + 10);
int n, tr[200];
std::vector<ll> a[3];

#define Odd(x) ((x) & 1)

ll Dis(int x, int y)
{
	std::set<ll> S; ll ans = 1e18;
	for (auto i : a[y]) S.insert(i);
	for (auto i : a[x])
	{
		auto it = S.lower_bound(i);
		if (it != S.end()) ans = std::min(ans, *it - i);
		if (it != S.begin()) ans = std::min(ans, i - *std::prev(it));
	}
	return ans;
}

int main()
{
#ifndef ONLINE_JUDGE
	file(cpp);
#endif
	tr['R'] = 0, tr['G'] = 1, tr['B'] = 2;
	std::ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cin >> n;
	for (int i = 1; i <= (n << 1); i++)
		{ ll x; char c; std::cin >> x >> c; a[tr[c]].push_back(x); }
	if (!(Odd(a[0].size()) || Odd(a[1].size()) || Odd(a[2].size()))) return puts("0"), 0;
	if (!Odd(a[0].size())) std::swap(a[0], a[2]);
	if (!Odd(a[1].size())) std::swap(a[1], a[2]);
	printf("%lld\n", std::min(Dis(0, 2) + Dis(1, 2), Dis(0, 1)));
	return 0;
}
