#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,fma,abm,mmx,avx,avx2")
#include <bits/stdc++.h>
#define all(V) V.begin(),V.end()
#define pi 3.1415926535897932384626
#define fi fixed<<setprecision(13)
#define ll long long 
#define rep(i, n) for (int i = 0; i < (ll)(n); i++)
#define Rep(i, n) for (ll i = 1; i <=(ll)(n); i++)
#define reps(i,k,n) for (ll i = (ll)k; i < (ll)(n); i++)
#define mp make_pair
#define mt make_tuple
using namespace std;
using V = vector<vector<int>>;
using Vc = vector<vector<char>>;
using Vb = vector<vector<bool>>;
using P = pair<int,int>;
using T = tuple<int,int,int>;
using Vp = vector<vector<pair<int,int>>>;
template<class T> void chmin(T& a,T b){ if(a > b) a=b; }
template<class T> void chmax(T& a,T b){ if(a < b) a=b; }
template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){ fill( (T*)array, (T*)(array+N), val );}
const ll infll = 1LL<<61;
const ll inf = 1<<30;
const int dx[4] = {0, 0, -1, 1};
const int dy[4] = {1, -1, 0, 0};
struct Edge{
  int to;
  ll w;
  Edge(int to,ll w) : to(to),w(w){}
};
ll nck(ll a,ll b){
  ll s=1,x=1;
  rep(i,b) s=(s*(a-i))/x,x++;
  return s;
}
using Graph = vector<vector<Edge>>;
/*-------------------------------------------------------------------*/

/*-------------------------------------------------------------------*/
int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  /*----------------------------------------------------*/
  int n; cin >> n;
  int r = 0, g = 0, b = 0;
  vector<pair<ll, string>> x(2 * n);
  rep(i, 2 * n){
    ll a; string s;
    cin >> a >> s;
    if(s == "R") r++;
    if(s == "G") g++;
    if(s == "B") b++;
    x[i] = mp(a, s);
  }
  if(r % 2 == 0 && g % 2 == 0 && b % 2 == 0){
    cout << 0 << "\n";
    return 0;
  }
  sort(all(x));
  ll RGmin = infll, RBmin = infll, GBmin = infll;
  if(r % 2 == 0){
    rep(i, 2 * n - 1){
      if(x[i].second != x[i+1].second ){
        if(x[i].second == "R" || x[i+1].second == "R"){
          if(x[i].second == "G" || x[i+1].second == "G") chmin(RGmin, abs(x[i].first - x[i+1].first));
          else chmin(RBmin,abs(x[i].first - x[i+1].first));
        }
        else chmin(GBmin,abs(x[i].first - x[i+1].first));
      }
    }
    cout << min(RGmin + RBmin, GBmin) << "\n";
    return 0;
  }
 if(g % 2 == 0){
    rep(i, 2 * n - 1){
      if(x[i].second != x[i+1].second ){
        if(x[i].second == "G" || x[i+1].second == "G"){
          if(x[i].second == "B" || x[i+1].second == "B") chmin(GBmin, abs(x[i].first - x[i+1].first));
          else chmin(RGmin,abs(x[i].first - x[i+1].first));
        }
        else chmin(RBmin,abs(x[i].first - x[i+1].first));
      }
    }
    cout << min(GBmin + RGmin, RBmin) << "\n";
    return 0;
  }
    if(b % 2 == 0){
    rep(i, 2 * n - 1){
      if(x[i].second != x[i+1].second ){
        if(x[i].second == "B" || x[i+1].second == "B"){
          if(x[i].second == "G" || x[i+1].second == "G") chmin(GBmin, abs(x[i].first - x[i+1].first));
          else chmin(RBmin,abs(x[i].first - x[i+1].first));
        }
        else chmin(RGmin,abs(x[i].first - x[i+1].first));
      }
    }
    cout << min(GBmin + RBmin, RGmin) << "\n";
    return 0;
  }
}

/*制約を確認しろllじゃなくて大丈夫か？*/
/* sample 保存
8
1 5 3 4 5 2 3 1







*/
#include<bits/stdc++.h>
using namespace std;
#define dbg(x) cerr << #x << " = " << x << endl
#define rep(i, a, b) for(int i = (a); i <= (b); ++ i)
#define MP make_pair
#define pb push_back
#define fi first
#define se second
typedef long long LL;
typedef unsigned long long u64;
typedef unsigned int u32;
template <typename Tp> void read(Tp &x){
	x = 0; int op = 1; char ch = getchar();
	while(!isdigit(ch)){ if(ch == '-') op = -1; ch = getchar(); }
	while(isdigit(ch)){ x = x*10+ch-'0'; ch = getchar(); } x *= op;
}
template <typename Tp> void CMax(Tp &x, Tp y){ if(y > x) x = y; }
template <typename Tp> void CMin(Tp &x, Tp y){ if(y < x) x = y; }

const int N = 1e5 + 10;

int n;
char s[N];

int main()
{
	scanf("%d", &n);
	scanf("%s", s + 1);
	if(s[1] != s[n]){
		printf("1");
		return 0;
	}
	rep(i, 2, n - 2) {
		if(s[i] != s[1] && s[i + 1] != s[1]){
			printf("2");
			return 0;
		}
	}
	printf("-1");
	return 0;
}
