/*
    author : aryan57
    created : 29-May-2021 18:09:38 IST
*/
#include <bits/stdc++.h>
using namespace std;

template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#ifndef ONLINE_JUDGE
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

#define int long long
#define X first
#define Y second
#define pb push_back
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define F(i, a, b) for (int i = a; i <= b; i++)
#define RF(i, a, b) for (int i = a; i >= b; i--)

const int mxn = 1e5;
const long long INF = 2e18;
const int32_t M = 1000000007;
// const int32_t M = 998244353;
const long double pie = acos(-1);

void solve_LOG()
{
    int n;
    cin>>n;

    vector <int> v[3];

    F(i,0,2*n-1)
    {
        int a;
        cin>>a;

        char x;
        cin>>x;

        if(x=='R')v[0].push_back(a);
        if(x=='G')v[1].push_back(a);
        if(x=='B')v[2].push_back(a);
    }

    if(sz(v[0])%2==0 && sz(v[1])%2==0 && sz(v[2])%2==0)
    {
        cout<<0;
        cout<<"\n";
        return;
    }

    int x=-1;
    F(i,0,2)
    {
        if(sz(v[i])%2==0)
        {
            x=i;
            break;
        }
    } 

    assert(x!=-1);

    if(x==1)
    {
        swap(v[1],v[2]);
    }
    if(x==0)
    {
        swap(v[0],v[2]);
    }

    sort(all(v[0]));
    sort(all(v[1]));

    int ans=INF;

    for(int x : v[0])
    {
        int pos=lower_bound(all(v[1]),x)-v[1].begin();

        F(i,pos-3,pos+3)
        {
            if(i<0 || i>=sz(v[1]))continue;
            ans=min(abs(x-v[1][i]),ans);
        }
    }

    assert(ans!=INF);

    vector<pair<int,int> > vec(sz(v[2]));
    int mnx=INF;
    int mnx_pos=-1;
    int mny=INF;
    int mny_pos=-1;
    F(j,0,sz(v[2])-1)
    {
        int pos=lower_bound(all(v[0]),v[2][j])-v[0].begin();
        int mn=INF;
        F(i,pos-3,pos+3)
        {
            if(i<0 || i>=sz(v[0]))continue;
            mn=min(abs(v[2][j]-v[0][i]),mn);
        }

        // assert(mn!=INF);
        vec[j].X=mn;

        if(mnx>mn)
        {
            mnx=mn;
            mnx_pos=j;
        }


        pos=lower_bound(all(v[1]),v[2][j])-v[1].begin();
        mn=INF;
        F(i,pos-3,pos+3)
        {
            if(i<0 || i>=sz(v[1]))continue;
            mn=min(abs(v[2][j]-v[1][i]),mn);
        }

        // assert(mn!=INF);
        vec[j].Y=mn;

        if(mny>mn)
        {
            mny=mn;
            mny_pos=j;
        }
    }

    if(mnx_pos!=mny_pos)
    {
        ans=min(ans,mnx+mny);
    }else
    {
        F(i,0,sz(vec)-1)
        {
            if(i!=mnx_pos)ans=min(ans,mnx+vec[i].Y);
            if(i!=mny_pos)ans=min(ans,mny+vec[i].X);
        }
    }                      

    cout<<ans;
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
#ifndef ONLINE_JUDGE
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
#endif
#ifdef ARYAN_SIEVE
    sieve();
#endif
#ifdef ARYAN_SEG_SIEVE
    segmented_sieve();
#endif
#ifdef ARYAN_FACT
    fact_init();
#endif
    // cout<<fixed<<setprecision(10);
    int _t=1;
    // cin>>_t;
    for (int i=1;i<=_t;i++)
    {
        // cout<<"Case #"<<i<<": ";
        solve_LOG();
    }
    return 0;
}
//	parsed : 29-May-2021 18:09:30 IST
//#pragma GCC optimize(3)
#include<bits/stdc++.h>
#define SZ(x) ((int)x.size())
#define uni(x) sort(all(x)),x.resize(unique(all(x))-x.begin());
#define GETPOS(c,x) (lower_bound(all(c),x)-c.begin())
#define lown1(x,val) low(in(x),val)-x
#define lowm1(x,val) low(im(x),val)-x
#define low1(x,nums,val) low(x+1,x+nums+1,val)-x
#define mst(x,val) memset((x),val,sizeof((x)))
#define ls rt<<1
#define rs rt<<1|1
#define lson rt<<1,l,M
#define rson rt<<1|1,M+1,r
#define PI acos(-1)
#define MM int M=(l+r)>>1;
#define fu(i,r,t) for(int i=r;i<=t;i++)
#define fd(i,r,t) for(int i=r;i>=t;i--)
#define fh(i,be,e) for(int i=head[be];~i;i=e[i].next)
#define fa(i,V) for(auto i:V)
#define far(i,V) for(auto &i:V)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define lcm(a,b) ((a)*(b))/__gcd(a,b)
#define cp(i,ans) printf("%.if",ans);
#define cpp(i,ans) cout<<setprecision(i)<<fixed<<ans<<endl;
#define ppb pop_back
#define ppf pop_front
#define pb push_back
#define pf push_front
#define pq priority_queue
#define lowbit(x) ((x)&(-x))
#define all(V) V.begin(),V.end()
#define ms multiset
#define mod(x) (((x)<0)?(x)%mo_num+mo_num:(x)%mo_num)
#define vc vector
#define vct vector<int>
#define SET set<int>
#define dq deque<int>
#define out(i) cout<<(i)<<endl;
#define fi first
#define se second
#define fun(i) fu(i,1,n)
#define fut(i) fu(i,1,t)
#define fum(i) fu(i,1,m)
#define ld long double
#define umap unordered_map
#define Umap unordered_map<int,int>
#define P pair<int,int>
#define mk make_tuple
#define eps 1e-6
//Remember cancel"#define endl '\n'" in interactive questions or use "<<flush"
#define endl '\n'
#define low lower_bound
#define upp upper_bound
#define yn(key) out(key?"YES":"NO")
//#define yn(key) out(key?"Yes":"No")
#define in(i) i+1,i+1+n
#define im(i) i+1,i+1+m
#define ik(i,k) i+1,i+1+k
#define bffs(i) __builtin_ffs(i)
#define bcount(i) __builtin_popcount(i)
#define bone(i) ((1<<i)-1)
#define db double
#define ll long long
#define got(container,num) get<num-1>(container)
#define int long long
#define print(a,n) fun(i)cout<<a[i]<<(i!=n?' ':endl);
#define outcase(x) cout<<"Case #"<<(++case_of_T)<<": "<<(x)<<endl;
#define ptcase(x) printf("Case #%d: %d\n",++case_of_T,x);
#define plcase(x) printf("Case #%lld: %lld\n",++case_of_T,x);
using namespace std;
//Remember to cancel the line below and declare INT=INT_MAX/2; when you want to change long to int
const int INF=LLONG_MAX/4,SINF=0x3f3f3f3f,Lim=1<<20,MINF=LLONG_MAX;
//const int INF=INT_MAX/4,SINF=0x3f;
// use C:printf("%.16f", x);  ->  printf("%.10f", x); can accelerate the program
const int dx[]={0,0,-1,1},dy[]={-1,1,0,0};//down up left right
const int maxn=1e6+1e5;
int mo_num=1e9+7;
//const int mo_num=998244353;
int n,m,t,a[maxn],b[maxn],ans,case_of_T;

void solve()
{
    cin>>n;
    fun(i)cin>>a[i];
    int now=0;
    fu(i,2,1000)
    {
        int num=0;
        fun(j)num+=a[j]%i==0;
        if(num>now)
        {
            now=num;ans=i;
        }
    }
    out(ans)
    return ;
}
main()
{
    IOS
    int T=1;
    //cin>>T;
    while(T--)solve();
    return 0;
}
