#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<tuple>
#include<cmath>
#include<iomanip>


using namespace std;
typedef long long ll;
typedef vector<ll> v;
typedef vector<vector<ll>> vv;


#define MOD 1000000007
#define INF 1001001001
#define MIN -1001001001
#define rep(i,k,N) for(int i=k;i<N;i++)
#define MP make_pair
#define MT make_tuple //tie,make_tuple は別物
#define PB push_back
#define PF push_front
#define all(x) (x).begin(),(x).end()


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


int main(){

    ll N;
    cin>>N;
    ll N1=0,N2=0,N3=0;
    v a(2*N);
    ll ev;
    vector<char> c(2*N);
    rep(i,0,2*N){
        cin>>a[i]>>c[i];
        if(c[i] == 'R')N1++;
        if(c[i] == 'G')N2++;
        if(c[i] == 'B')N3++;
    }
    if(N1 %2 == 0 && N2 %2 == 0 && N3 %2 == 0){
        cout<<0;
        return 0;
    }
    ll r=1,g=2,b=3;
    if(N1%2 == 0)ev = 1;
    if(N2%2 == 0){g=1;r=2;swap(N1,N2);}
    if(N3%2 == 0){r=3;b=1;swap(N1,N3);}

    vector<pair<ll,ll>> Arg;
    vector<pair<ll,ll>> Arb;
    vector<pair<ll,ll>> Agb;
    
    rep(i,0,2*N){
        pair<ll,ll> tmp;
        tmp.first = a[i];
        ll d;
        if(c[i] == 'R')d=r;
        if(c[i] == 'G')d=g;
        if(c[i] == 'B')d=b;
        tmp.second = d;
        if(d==1){Arg.push_back(tmp);Arb.push_back(tmp);}
        if(d==3){Arb.push_back(tmp);Agb.push_back(tmp);}
        if(d==2){Arg.push_back(tmp);Agb.push_back(tmp);}
    }
    ll rg = 1,gb=2,br=3;
    sort(all(Arg));
    sort(all(Arb));
    sort(all(Agb));
    vector<pair<ll,ll>> drg;
    vector<pair<ll,ll>> dgb;
    vector<pair<ll,ll>> drb;
    ll Nr;
    Nr = 0;
    rep(i,0,Arg.size()){
        if(i==0 && Arg[i].second == 1)Nr++;
        if(Arg[i+1].second == 1)Nr++;
        if(Arg[i].second == Arg[i+1].second)continue;
        drg.emplace_back(abs(Arg[i].first-Arg[i+1].first),Nr);
    }
    Nr = 0;
    rep(i,0,Arb.size()){
        if(i==0 && Arb[i].second == 1)Nr++;
        if(Arb[i+1].second == 1)Nr++;
        if(Arb[i].second == Arb[i+1].second)continue;
        drb.emplace_back(abs(Arb[i].first-Arb[i+1].first),Nr);
    }
    rep(i,0,Agb.size()){
        if(Agb[i].second == Agb[i+1].second)continue;
        dgb.emplace_back(abs(Agb[i].first-Agb[i+1].first),Nr);
    }
    
    sort(all(drg));
    sort(all(dgb));
    sort(all(drb));
    ll rr;
    if(drg[0].second == drb[0].second){
        if(drg[0].second == drb[1].second && drg[1].second == drb[0].second){
            rr = min(drg[0].first + drb[2].first,drg[2].first + drb[0].first);
        }
        else if(drg[0].second == drb[1].second){
            rr = min(drg[0].first + drb[2].first,drg[1].first + drb[0].first);
        }
        else if(drg[1].second == drb[0].second){
            rr = min(drg[1].first + drb[0].first,drg[0].first + drb[2].first);
        }
        else{
            rr = min(drg[0].first + drb[1].first,drg[1].first + drb[0].first);
        }
    }
    else{
        rr = drg[0].first + drb[0].first;
    }
    rr = min(rr,dgb[0].first);

    cout<<rr;
    return 0;
}
//Generated at 2020-10-03 20:08:03 UTC+8
//Created by Alphagocc
#include<bits/stdc++.h>
#ifdef USE_PBDS
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/priority_queue.hpp>
#endif
using namespace std;
#define FI __inline__ __attribute__((always_inline))
#define TN typename
#define TP template
#define TS *this
class IO{static const int bufSize=1<<18;char inBuf[bufSize],outBuf[bufSize];char*ip1=inBuf,*ip2=inBuf;int ok=1,op1=-1,op2=bufSize-1;
TP <TN T>FI void __RI(T&x){int ch=gc(),neg=1;x=0;for(;!(ch==EOF||isdigit(ch)||ch=='-');ch=gc());if(ch==EOF){ok=0;return;}if(ch=='-')neg=-1,ch=gc();for(;isdigit(ch);ch=gc())x=x*10+(ch-48)*neg;}
TP <TN T>FI void __RC(T&x){unsigned char ch;while(isspace(ch=gc()));if(ch==EOF){ok=0;return;}x=ch;}FI void __RS(string&x){char ch;x.clear();for(ch=gc();isspace(ch);ch=gc());if(ch==EOF){ok=0;return;}for(;!isspace(ch);ch=gc())x.push_back(ch);}
public:
FI int gc(){return ip1==ip2 &&(ip2 =(ip1=inBuf)+fread(inBuf,1,bufSize,stdin),ip1==ip2)?EOF:*ip1++;}
FI IO&R(char&x){__RC(x);return TS;}FI IO& R(unsigned char&x){__RC(x);return TS;}FI IO&R(string&x){__RS(x);return TS;}TP<TN T1,TN T2>FI IO&R(pair<T1,T2>&x){R(x.first),R(x.second);return TS;}TP<TN T>FI IO&R(vector<T>&x){for(auto &i:x)R(i);return TS;}
TP <TN T,TN...Args>FI IO&RA(T*a,int n){for(int i=0;i<n;++i)R(a[i]);return TS;}TP<TN T,TN...Args>FI IO&R(T&x,Args&...args){R(x),R(args...);return TS;}
TP <TN T,TN...Args>FI IO&RA(T*a,int n,Args...args){for(int i=0;i<n;++i)RA(a[i],args...);return TS;}TP<TN T>FI IO&R(T&x){static_assert(is_integral<T>::value,"Unsupported types");if(is_integral<T>::value)__RI(x);return TS;}
private:
char space=' ';TP<TN T>FI void __WI(T x){static char buf[64];static int len=-1;if(x>=0){do{buf[++len]=x%10+48,x/=10;}while(x);}else{pc('-');do{buf[++len]=-(x%10)+48,x/=10;}while(x);}while(len>=0){pc(buf[len]),--len;}}
public:
FI void pc(const char&x){if(op1==op2)flush();outBuf[++op1]=x;}
FI void flush(){fwrite(outBuf,1,op1+1,stdout),op1=-1;}FI IO&W(const char&x){pc(x);return TS;}FI IO&W(const char*x){while(*x!='\0')pc(*(x++));return TS;}FI IO&W(const string&x){W(x.c_str());return TS;}
TP<TN T>FI IO& W(const vector<T>&x){for(auto&i:x)WS(i);WL();return TS;}FI IO&WL(){W('\n');return TS;}TP<TN T1,TN T2>FI IO& W(const pair<T1,T2>&x){WS(x.first),W(x.second);return TS;}
TP<TN T>FI IO& WL(const T&x){W(x),W('\n');return TS;}FI IO&WS(){W(' ');return TS;}TP<TN T>FI IO& WS(const T&x){W(x),W(space);return TS;}
TP<TN T>FI IO& WA(T* a,int n){for(int i=0;i<n;i++)WS(a[i]);WL();return TS;}TP<TN T,TN...Args>FI IO& W(const T&x,const Args&...args){W(x),W(space),W(args...);return TS;}
TP<TN...Args>FI IO& WL(const Args&...args){W(args...),W('\n');return TS;}TP<TN T,TN...Args>FI IO& WA(T* a,int n,Args...args){for(int i=0;i<n;i++)WA(a[i],args...);return TS;}
TP<TN T>FI IO& W(const T&x){static_assert(is_integral<T>::value,"Unsupported types");if(is_integral<T>::value)__WI(x);return TS;}
TP<TN T>FI IO&operator>>(T&x){R(x);return TS;}TP<TN T>FI IO&operator<<(const T&x){W(x);return TS;}bool good(){return ok;}IO(){}~IO(){flush();}}io;
#undef TN 
#undef TP
#undef FI
#undef TS
#define REP(i,x,y)for(int i=x;i<y;i++)
#define ALL(x)x.begin(),x.end()
const int32_t INF=0x3f3f3f3f;
const int64_t INFL=0x3f3f3f3f3f3f3f3fLL;

int main()
{
    int n;
    io >> n;
    vector<char> s(n);
    io >> s;
    int ans = 0;
    for (int i = 0; i < n; i++) {
        int a, t, g, c;
        a = t = g = c = 0;
        for (int j = i; j < n; j++) {
            switch (s[j]) {
            case 'A': a++; break;
            case 'T': t++; break;
            case 'G': g++; break;
            case 'C': c++; break;
            default: break;
            }
            // io.WL(a, t, g, c);
            if (a == t && g == c) ans++;
        }
    }
    io.WL(ans);
    return 0;
}