#include<bits/stdc++.h>
using namespace std;
#define lli long long int
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define test lli t;cin>>t;while(t--)
#define vll vector<lli>
#define vpll vector<pair<lli,lli>>
#define prq priority_queue<lli>
#define psq priority_queue<lli,vector<lli>,greater<lli>>
#define pll pair<lli,lli>
#define pb push_back
#define bs binary_search
#define lb lower_bound
#define ub upper_bound
#define ff first
#define ss second
#define nl "\n"
#define ms0(s) memset(s,0,sizeof(s))
#define mod 1000000007
#define ma 1000000000000000000
#define mi -1000000000000000000
int main()
{
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fastio
  lli n, i, j;
  cin >> n;
  vll a, b, c;
  char ch;
  for (i = 0; i < 2 * n; i++)
  {
    cin >> j;
    cin >> ch;
    if (ch == 'R')
      a.pb(j);
    else if (ch == 'G')
      b.pb(j);
    else
      c.pb(j);
  }
  if (a.size() % 2 == 0 && b.size() % 2 == 0 && c.size() % 2 == 0)
  {
    cout << 0;
  }
  else if (a.size() % 2 == 0)
  {
    sort(b.begin(), b.end());

    sort(c.begin(), c.end());
    lli ans = ma;
    for (auto i : b)
    {
      j = upper_bound(c.begin(), c.end(), i) - c.begin();
      if (j < c.size())
        ans = min(ans, abs(i - c[j]));
      j--;
      if (j >= 0)
        ans = min(ans, abs(i - c[j]));
    }
    if (a.size() > 0)
    {
      sort(a.begin(), a.end());
      lli an1 = ma;
      for (auto i : a)
      {
        j = upper_bound(c.begin(), c.end(), i) - c.begin();
        if (j < c.size())
          an1 = min(an1, abs(i - c[j]));
        j--;
        if (j >= 0)
          an1 = min(an1, abs(i - c[j]));
      }
      lli an2 = ma;
      for (auto i : b)
      {
        j = upper_bound(a.begin(), a.end(), i) - a.begin();
        if (j < a.size())
          an2 = min(an2, abs(i - a[j]));
        j--;
        if (j >= 0)
          an2 = min(an2, abs(i - a[j]));
      }
      ans = min(ans, an1 + an2);
    }
    cout << ans;
  }
  else if (b.size() % 2 == 0)
  {
    sort(a.begin(), a.end());
    lli ans = ma;
    sort(c.begin(), c.end());
    for (auto i : a)
    {
      j = upper_bound(c.begin(), c.end(), i) - c.begin();
      if (j < c.size())
        ans = min(ans, abs(i - c[j]));
      j--;
      if (j >= 0)
        ans = min(ans, abs(i - c[j]));
    }
    if (b.size() > 0)
    {
      sort(b.begin(), b.end());
      lli an1 = ma;
      for (auto i : b)
      {
        j = upper_bound(c.begin(), c.end(), i) - c.begin();
        if (j < c.size())
          an1 = min(an1, abs(i - c[j]));
        j--;
        if (j >= 0)
          an1 = min(an1, abs(i - c[j]));
      }
      lli an2 = ma;
      for (auto i : b)
      {
        j = upper_bound(a.begin(), a.end(), i) - a.begin();
        if (j < a.size())
          an2 = min(an2, abs(i - a[j]));
        j--;
        if (j >= 0)
          an2 = min(an2, abs(i - a[j]));
      }
      ans = min(ans, an1 + an2);
    }
    cout << ans;
  }
  else if (c.size() % 2 == 0)
  {
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    lli ans = ma;
    for (auto i : b)
    {
      j = upper_bound(a.begin(), a.end(), i) - a.begin();
      if (j < a.size())
        ans = min(ans, abs(i - a[j]));
      j--;
      if (j >= 0)
        ans = min(ans, abs(i - a[j]));
    }
    if (c.size() > 0)
    {
      sort(c.begin(), c.end());
      lli an1 = ma;
      for (auto i : a)
      {
        j = upper_bound(c.begin(), c.end(), i) - c.begin();
        if (j < c.size())
          an1 = min(an1, abs(i - c[j]));
        j--;
        if (j >= 0)
          an1 = min(an1, abs(i - c[j]));
      }
      lli an2 = ma;
      for (auto i : b)
      {
        j = upper_bound(c.begin(), c.end(), i) - c.begin();
        if (j < c.size())
          an2 = min(an2, abs(i - c[j]));
        j--;
        if (j >= 0)
          an2 = min(an2, abs(i - c[j]));
      }
      ans = min(ans, an1 + an2);
    }
    cout << ans;
  }

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

#define   fi(a,b) for(int i=a;i<b;i++)
#define   fj(a,b) for(int j=a;j<b;j++)
#define   ff first
#define   ss second
#define   ll long long
#define   ld long double
#define   ull unsigned long long
#define   bp(x) __builtin_popcount(x)
#define   pr(x) for(auto it: x) cout<<it<<" "; cout<<endl;
#define   getMax(x) max_element(x.begin(),x.end())
#define   getMin(x) min_element(x.begin(),x.end())
#define   endl "\n"
typedef   vector<int> vi;
typedef   vector< pair<int, int> > vii;
typedef   vector<long long> vl;
typedef   pair<int, int> pii;
typedef   pair<ll, ll> pll;
typedef   vector< pair<ll, ll> > vll;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// auto dist = uniform_int_distribution<int>(l, r);
// int dx[] = {1, 0, -1, 0};
// int dy[] = {0, 1, 0, -1};
// int dx[] = { -1, 0, 1, 1, 1, 0, -1, -1};
// int dy[] = { -1, -1, -1, 0, 1, 1, 1, 0};


void omae_wa_mou_shindeiru(int tc) {
	ll n;
	cin >> n;
	n *= 2;
	vector<vl> v(3);
	fi(0, n) {
		ll temp;
		cin >> temp;
		char ch;
		cin >> ch;
		if (ch == 'R') v[0].push_back(temp);
		else if (ch == 'G') v[1].push_back(temp);
		else v[2].push_back(temp);
	}
	vl rem;
	bool check = 1;
	fi(0, 3) {
		sort(v[i].begin(), v[i].end());
		if (v[i].size() % 2) {
			rem.push_back(i);
		}
	}
	if (rem.size() == 0) {
		cout << 0 << endl;
		return;
	}
	vector<vl>arr(3, vl(3, 1e18));
	ll ans = 1e18;

	for (auto it : v[rem[0]]) {
		auto ind = lower_bound(v[rem[1]].begin(), v[rem[1]].end(), it) - v[rem[1]].begin();
		if (ind != v[rem[1]].size()) {
			ans = min(ans, abs(v[rem[1]][ind] - it));
		}
		if (ind != 0) {
			ans = min(ans, abs(v[rem[1]][ind - 1] - it));
		}
	}

	const ll INF = 1e18;
	int other = 3 - rem[1] - rem[0];
	int sz = v[other].size();
	vector<array<ll, 2>>dp(sz + 1, array<ll, 2>({INF, INF}));
	fi(0, sz) {
		ll it = v[other][i];
		auto ind = lower_bound(v[rem[1]].begin(), v[rem[1]].end(), it) - v[rem[1]].begin();
		ll temp1 = 1e18, temp2 = 1e18;
		if (ind != v[rem[1]].size()) {
			temp1 = abs(v[rem[1]][ind] - it);
		}
		if (ind != 0) {
			temp1 = min(temp1, abs(v[rem[1]][ind - 1] - it));
		}
		ind = lower_bound(v[rem[0]].begin(), v[rem[0]].end(), it) - v[rem[0]].begin();
		if (ind != v[rem[0]].size()) {
			temp2 = min(temp2, abs(v[rem[0]][ind] - it));
		}
		if (ind != 0) {
			temp2 = min(temp2, abs(v[rem[0]][ind - 1] - it));
		}
		ans = min(ans, temp2 + dp[i][1]);
		ans = min(ans, dp[i][0] + temp1);
		dp[i + 1][1] = min(dp[i][1], temp1);
		dp[i + 1][0] = min(dp[i][0], temp2);
	}
	cout << ans << endl;

}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	int tc = 1;
	// cin >> tc;
	fi(1, tc + 1) {
		omae_wa_mou_shindeiru(i);
	}
}
