#include <bits/stdc++.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <list>
#define eps 10e-14
//cout<<fixed<<setprecision(15)<<
//s=to_string(100);
//vector<vector<char>>c(h,vector<char>(w));
//next_permutation(orig.begin(),orig.end());
using namespace std;
int gcd(int x,int y){
  while(y>0){ 
    int r=x%y;
      x=y;
      y=r;
  }
  return x;
}
int lcm(int a,int b){
  int r=gcd(a,b);
  return a*b/r;
}
template<typename T, typename U>
void primeFactorization(map<T, U> &primes, T N) {
  T n=N;
  U num=0;
  while(n%2==0){
    num++;
    n/=2;
  }
  if(num!=0){
    primes.emplace(2,num);
  }
  for(T i=3;i*i<=N;i+=2) {
    num=0;
    while(n%i==0){
      num++;
      n/=i;
    }
    if(num!=0){
      primes.emplace(i, num);
    }
  }
  if(n!=1){
    primes.emplace(n,1);
  }
}
template<typename TypeInt>
string Itoa(const TypeInt v, int base){
    static const char table[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    string ret;
    static numeric_limits<TypeInt> t;
    TypeInt n = v;
    if (t.is_signed) {
        if (v < 0) n *= -1;
    }
    while (n >= base) {
        ret += table[n%base];
        n /= base;
    }
    ret += table[n];
    if (t.is_signed) {
        if (v < 0 && base == 10) ret += '-';
    }
    // 文字列の順番を逆にする
    std::reverse(ret.begin(), ret.end());
    return ret;
}
int hanoi(int n){
  return pow(2,n)-1;
}

int main() {
  long long n,k,count=0;
  cin>>n;
  vector<long long>a(n),b(n);
  map<long long,long long>m;
  for(int i=0;i<n;i++){
    cin>>a.at(i);
    m[a.at(i)%200]++;
  }
  for(auto t:m){
    if(t.second>=2){
      for(int i=1;i<t.second;i++)count+=i;
    }
  }
  cout<<count<<endl;
}
#include <iostream>
#include <vector>
#include <set>

using namespace std;

typedef long long ll;

const ll INF = 1e18 + 2000;

ll mdist (const set<pair<ll, int>> &A, const set<pair<ll, int>> &B) {
  ll ans = INF;
  for (auto it = A.begin(); it != A.end(); it++) {
    auto oth = B.lower_bound(*it);
    if (oth != B.end()) {
      ans = min(ans, oth->first - it->first);
    }

    if (oth != B.begin()) {
      ans = min(ans, it->first - prev(oth)->first);
    }
  }
  return ans;
}

int main () {
  int n;
  cin >> n;
  n *= 2;

  set<pair<ll, int>> rs, gs, bs;
  for (int i = 0; i < n; i++) {
    ll a;
    char c;
    cin >> a >> c;

    auto pr = make_pair(a, i);
    
    switch(c) {
    case 'R':
      rs.insert(pr);
      break;
    case 'G':
      gs.insert(pr);
      break;
    case 'B':
      bs.insert(pr);
      break;
    }
  }

  if ((int) rs.size() % 2 == 0 && (int) gs.size() % 2 == 0 && (int) bs.size() % 2 == 0) {
    cout << 0 << endl;
    return 0;
  }

  if (bs.size() % 2 == 0) {
    swap(bs, gs);
  }

  if (rs.size() % 2 == 0) {
    swap(rs, gs);
  }

  cout << min(mdist(rs, bs), mdist(rs, gs) + mdist(bs, gs)) << endl;
}
