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

// Template Headers {{{
using pii = pair<int, int>; using vpii = vector<pii>;
using vi = vector<int>; using vvi = vector<vi>;
using ll = long long; using vll = vector<long long>;
template <class T> using min_queue = priority_queue<T, vector<T>, greater<T>>;
template <class T> istream &operator>>(istream &, vector<T> &);
template <class T> ostream &operator<<(ostream &, const vector<T> &);
template <class T, class U> istream &operator>>(istream &, pair<T, U> &);
template <class T, class U> ostream &operator<<(ostream &, const pair<T, U> &);
template <class T> struct Inf { static constexpr T inf() { return std::numeric_limits<T>::has_infinity() ? std::numeric_limits<T>::infinty() : std::numeric_limits<T>::max(); } };
template <> struct Inf<int> { static constexpr int inf() { return 0x3f3f3f3f; } };
template <> struct Inf<long long> { static constexpr long long inf() { return 0x3f3f3f3f3f3f3f3fLL; } };
constexpr int INF = Inf<int>::inf(); constexpr ll BINF = Inf<ll>::inf();
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// }}}

int solve() {
	int n; string s; cin >> n >> s;
	using vvvi = vector<vvi>;
	using vvvvi = vector<vvvi>;
	vi vy, vg, vr;
	for (int i = 0; i < n; i++) {
		if (s[i] == 'Y') vy.push_back(i);
		else if (s[i] == 'G') vg.push_back(i);
		else vr.push_back(i);
	}
	int ny = vy.size(), ng = vg.size(), nr = vr.size();
	const int M = 4;
	vvvvi dp(ny + 1, vvvi(ng + 1, vvi(nr + 1, vi(M, INF))));
	dp[0][0][0][3] = 0;
	for (int i = 0; i <= ny; i++) for (int j = 0; j <= ng; j++) for (int k = 0; k <= nr; k++) {
		for (int l = 0; l < M; l++) {
			if (i > 0 && l != 0) {
				int costG = j - distance(vg.begin(), lower_bound(vg.begin(), vg.begin() + j, vy[i - 1]));
				int costR = k - distance(vr.begin(), lower_bound(vr.begin(), vr.begin() + k, vy[i - 1]));
				dp[i][j][k][0] = min(dp[i][j][k][0], dp[i - 1][j][k][l] + costG + costR);
			}
			if (j > 0 && l != 1) {
				int costY = i - distance(vy.begin(), lower_bound(vy.begin(), vy.begin() + i, vg[j - 1]));
				int costR = k - distance(vr.begin(), lower_bound(vr.begin(), vr.begin() + k, vg[j - 1]));
				dp[i][j][k][1] = min(dp[i][j][k][1], dp[i][j - 1][k][l] + costY + costR);
			}
			if (k > 0 && l != 2) {
				int costY = i - distance(vy.begin(), lower_bound(vy.begin(), vy.begin() + i, vr[k - 1]));
				int costG = j - distance(vg.begin(), lower_bound(vg.begin(), vg.begin() + j, vr[k - 1]));
				dp[i][j][k][2] = min(dp[i][j][k][2], dp[i][j][k - 1][l] + costY + costG);
			}
		}
	}
	int ans = INF;
	for (int i = 0; i < M; i++) ans = min(ans, dp[ny][ng][nr][i]);
	cout << (ans >= INF ? -1 : ans) << endl;
	return 0;
}

// Template Main {{{
int main() {
	ios::sync_with_stdio(0);
	// precompute();
	// int t; cin >> t; for (int i=1;i<=t;i++)
	solve();
	// cout << "Case #" << i << ": ", solve();
	return 0;
}

template <class T> istream &operator>>(istream &is, vector<T> &v) {
	for (auto it=v.begin();it!=v.end();++it) is >> *it;
	return is;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
	for (auto it=v.begin();it!=v.end();) os << *it, os << " \n"[++it==v.end()];
	return os;
}

template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) {
	return is >> p.first >> p.second;
}

template <class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) {
	return os << p.first << ' ' << p.second;
}
// }}}

// vim: fdm=marker


Measured memory_bytes: 137132