#include<cstdio>
#include<iostream>
#include<algorithm>
#include<utility>
#include<vector>
#include<queue>
#include<functional>
#include<map>
#include<set>
#include<cstring>
#include<string>
#include<stack>
#include<malloc.h>
#include<math.h>
using namespace std;
//#define int long long
typedef pair<int, int> pii;
typedef pair<int, char> pic;
typedef priority_queue<pii, vector<pii>, greater<pii> > RPQ;
#define mk make_pair
#define INF (1 << 30)
#define INFL (1ll << 60ll)
#define mod 1000000007

int N;
int col[440];
//int dp[4][405][405][405];//x+y+z番目まで並べ終わっていてcが先頭で1をx個、2をy個、3をz個並べ終わっているときから並べ終わるまでの最小手数
int ****dp;

int pos[4][440];
int csum[4];

int bit[440];

int sum(int i){
    int s = 0;
    while(i > 0){
        s += bit[i];
        i -= i & -i;
    }
    return s;
}

void add(int i, int x){
    while(i <= N){
        bit[i] += x;
        i += i & -i;
    }
}

int dfs(int c, int x, int y, int z){
    if(x + y + z == N)return 0;
    if(~dp[c][x][y][z]){
        return dp[c][x][y][z];
    }
    int ret = INF;
    if(x < csum[1] && c != 1){
        add(pos[1][x + 1], 1);
        ret = min(ret, dfs(1, x + 1, y, z) + pos[1][x + 1] - sum(pos[1][x + 1]));
        add(pos[1][x + 1], -1);
    }
    if(y < csum[2] && c != 2){
        add(pos[2][y + 1], 1);
        ret = min(ret, dfs(2, x, y + 1, z) + pos[2][y + 1] - sum(pos[2][y + 1]));
        add(pos[2][y + 1], -1);
    }
    if(z < csum[3] && c != 3){
        add(pos[3][z + 1], 1);
        ret = min(ret, dfs(3, x, y, z + 1) + pos[3][z + 1] - sum(pos[3][z + 1]));
        add(pos[3][z + 1], -1);
    }
    return dp[c][x][y][z] = ret;
}

signed main(){
    cin >> N;
    //cout << (int)sizeof(dp) / 1000 / 1000 << endl;
    int cnt1 = 0, cnt2 = 0, cnt3 = 0;
    for(int i = 1; i <= N; i++){
        char a;
        cin >> a;
        if(a == 'R'){
            col[i] = 1;
            cnt1++;
            pos[1][cnt1] = i;
            csum[1]++;
        }
        else if(a == 'G'){
            col[i] = 2;
            cnt2++;
            pos[2][cnt2] = i;
            csum[2]++;
        }
        else {
            col[i] = 3;
            cnt3++;
            pos[3][cnt3] = i;
            csum[3]++;
        }
    }
    dp = (int ****)malloc(4 * sizeof(int***));
    for(int i = 0; i < 4; i++){
        dp[i] = (int ***)malloc((csum[1] + 10) * sizeof(int **));
    }
    for(int i = 0; i < 4; i++){
        for(int j = 0; j < csum[1] + 10; j++){
            dp[i][j] = (int **)malloc((csum[2] + 10) * sizeof(int *));
        }
    }
    for(int i = 0; i < 4; i++){
        for(int j = 0; j < csum[1] + 10; j++){
            for(int k = 0; k < csum[2] + 10; k++){
                dp[i][j][k] = (int *)malloc((csum[3] + 10) * sizeof(int));
            }
        }
    }
    for(int i = 0; i < 4; i++){
        for(int j = 0; j < csum[1] + 10; j++){
            for(int k = 0; k < csum[2] + 10; k++){
                for(int r = 0; r < csum[3] + 10; r++){
                    dp[i][j][k][r] = -1;
                }
            }
        }
    }
    int ans = dfs(0, 0, 0, 0);
    if(ans >= INF)ans = -1;
    cout << ans << endl;
    for(int i = 0; i < 4; i++){
        for(int j = 0; j < csum[1] + 10; j++){
            for(int k = 0; k < csum[2] + 10; k++){
                free(dp[i][j][k]);
            }
        }
    }


    return 0;
}

/*



*/


Measured memory_bytes: 50752