#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

static_assert(sizeof(long) == 8, "long must be 64-bit.");

int sindex(const string& S, char c, int start = 0)
{
    for (int i = start; ; ++i)
        if (S[i] == c)
            return i;
}

void set_min(int& a, int v)
{
    if (a > v)
        a = v;
}

int solve(int N, const string& S)
{
    const int INF = (N + 1) * (N + 1);

    int cr = 0, cg = 0, cy = 0;
    for (char c : S) {
        switch (c) {
        case 'R':
            ++cr;
            break;
        case 'G':
            ++cg;
            break;
        case 'Y':
            ++cy;
            break;
        }
    }

    vector<vector<vector<array<int, 3>>>> move_table(
        cr + 1, vector<vector<array<int, 3>>>(
            cg + 1, vector<array<int, 3>>(
                cy + 1)));
    for (int ur = 0; ur <= cr; ++ur) {
        for (int ug = 0; ug <= cg; ++ug) {
            string B;
            int rr = ur;
            int rg = ug;
            for (char c : S) {
                if (c == 'R' && rr > 0) {
                    --rr;
                } else if (c == 'G' && rg > 0) {
                    --rg;
                } else {
                    B.push_back(c);
                }
            }
            int lenb = (int)B.size();
            int ri = ur < cr ? sindex(B, 'R') : lenb;
            int gi = ug < cg ? sindex(B, 'G') : lenb;
            int yi = cy > 0 ? sindex(B, 'Y') : lenb;

            int roff = 0, goff = 0, yoff = 0;

            for (int uy = 0; uy < cy; ++uy) {
                move_table[ur][ug][uy] = {
                    ri - roff,
                    gi - goff,
                    yi - yoff,
                };

                if (ri > yi)
                    ++roff;
                if (gi > yi)
                    ++goff;
                ++yoff;

                yi = uy < cy - 1 ? sindex(B, 'Y', yi + 1) : lenb;
            }

            move_table[ur][ug][cy] = {
                ri - roff,
                gi - goff,
                yi - yoff,
            };
        }
    }

    vector<vector<array<int, 3>>> dp(
        cr + 1, vector<array<int, 3>>(
            cg + 1, {INF, INF, INF}));
    dp[0][0] = {0, 0, 0};

    for (int ut = 0; ut < N; ++ut) {
        vector<vector<array<int, 3>>> ndp(
            cr + 1, vector<array<int, 3>>(
                cg + 1, {INF, INF, INF}));

        for (int ur = 0; ur <= min(ut, cr); ++ur) {
            int rest = ut - ur;
            for (int ug = min(rest, cg); ug >= 0; --ug) {
                int uy = ut - ur - ug;
                if (uy > cy)
                    break;
                for (int col = 0; col < 3; ++col) {
                    int dv = dp[ur][ug][col];
                    if (dv == INF)
                        continue;
                    if (ur < cr && col != 0) {
                        set_min(ndp[ur + 1][ug][0],
                                dv + move_table[ur][ug][uy][0]);
                    }
                    if (ug < cg && col != 1) {
                        set_min(ndp[ur][ug + 1][1],
                                dv + move_table[ur][ug][uy][1]);
                    }
                    if (uy < cy && col != 2) {
                        set_min(ndp[ur][ug][2],
                                dv + move_table[ur][ug][uy][2]);
                    }
                }
            }
        }

        dp.swap(ndp);
    }

    int ans = min(dp[cr][cg][0],
                  min(dp[cr][cg][1], dp[cr][cg][2]));
    if (ans == INF)
        return -1;
    return ans;
}

int main()
{
    int N;
    cin >> N;
    string S;
    cin >> S;
    cout << solve(N, S) << '\n';
    return 0;
}



Measured memory_bytes: 32488