#include <bits/stdc++.h>
using namespace std;
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	int n;
	string s;
	cin>>n>>s;
	vector<int>dp(n,1e9);
	vector<vector<int> >diff(26);
	for(int i=0;i<26;i++)
		for(int j=0;j<n;j++)
			if(s[j]!=i+'a') diff[i].push_back(j);
	vector<priority_queue<int> >pq(26);
	for(int i=1;i<n;i++) {
		if(pq[s[i]-'a'].empty()) {
			for(int j=0;j<diff[s[i]-'a'].size()&&diff[s[i]-'a'][j]<i;j++)
				dp[i]=min(dp[i],(diff[s[i]-'a'][j]?dp[diff[s[i]-'a'][j]-1]:0)+1);
			pq[s[i]-'a'].push(-dp[i]);	
		}		
		else dp[i]=-pq[s[i]-'a'].top();
	}	
	if(dp[n-1]>=1e9) cout<<-1;
	else cout<<dp[n-1];
	return 0;
}	
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,sse4,fma")

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

using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;

using vl = vector<ll>;
using vi = vector<int>;
using vb = vector<bool>;

using pll = pair<ll, ll>;
using pii = pair<int, int>;
using vpi = vector<pii>;

using rng = default_random_engine;


template <typename T>
using minpq = priority_queue<T, vector<T>, greater<T>>;


#define F first
#define S second
#define lb lower_bound
#define ub upper_bound
#define np next_permutation
#define sz(v) int(v.size())
#define all(v) begin(v), end(v)
#define popcnt __builtin_popcount

const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};

const int INF = 2000069420;
const int M1 = 1'000'000'007;
const int M2 =   998'244'353;
const int maxn = 1'000'005;
const int maxg = 2'005;

inline ull absul(ull a, ull b) {
	return a < b ? b-a : a-b;
}

char ch;
int mp[256];
vector<ull> b[3];

void solve() {
	mp['R'] = 0;
	mp['G'] = 1;
	mp['B'] = 2;
	int n; cin >> n;
	for (ull a; cin >> a >> ch;)
		b[mp[ch]].push_back(a);
	
	vi ext;
	for (int i = 0; i < 3; ++i) {
		sort(all(b[i]));
		if (sz(b[i]) & 1)
			ext.push_back(i);
	}

	if (!sz(ext)) {
		cout << "0\n";
		return void();
	}

	assert(sz(ext) == 2);

	int i = ext[0], j = ext[1];
	ull mn = 18e18;
	// try combining two dogs from clans i and j
	for (const ull &v: b[i]) {
		auto it1 = lb(all(b[j]), v);
		auto it2 = ub(all(b[j]), v);
		if (it1 == b[j].end())--it1;
		if (it2 != b[j].begin())--it2;
		mn = min({mn, absul(*it1,v), absul(*it2,v)});
	}

	// Try combining two extra dogs to the third clan
	int k = 3-i-j;

	cerr << "mn = " << mn << '\n';
	
	if (!sz(b[k])) {
		cout << mn << '\n';
		return void();
	}
	
	
	ull mn1 = 18e18, mn2 = 18e18;

	for (const ull &v: b[i]) {
		auto it1 = lb(all(b[k]), v);
		auto it2 = ub(all(b[k]), v);
		if (it1 == b[k].end())--it1;
		if (it2 != b[k].begin())--it2;
		mn1 = min({mn1, absul(*it1,v), absul(*it2,v)});
	}

	for (const ull &v: b[j]) {
		auto it1 = lb(all(b[k]), v);
		auto it2 = ub(all(b[k]), v);
		if (it1 == b[k].end())--it1;
		if (it2 != b[k].begin())--it2;
		mn2 = min({mn2, absul(*it1,v), absul(*it2,v)});
	}

	cerr << "mn1 = " << mn1 << '\n';
	cerr << "mn2 = " << mn2 << '\n';
	cerr << "mn1+mn2 = " << mn1+mn2 << '\n';

	cout << min(mn, mn1+mn2) << '\n';
}



int main() {
	cin.tie(nullptr) ->
	sync_with_stdio(false);

	int T = 1; // cin >> T;
	while (T--) solve();
}