#include <bits/stdc++.h>

using namespace std;

int main(){
    long long N;
    cin >> N;
    set<long long> A,B,C;
    vector<vector<long long> > BB;
    vector<long long> v0;
    vector<long long> Ac,Cc;
    for(long long i=0; i<N; i++){
        Ac.push_back(0);
        BB.push_back(v0);
        Cc.push_back(0);
    }
    long long p;
    for(long long i=0; i<N; i++){
        cin >> p;
        Ac.at(p-1)++;
        A.insert(p-1);
    }
    for(long long i=0; i<N; i++){
        cin >> p;
        BB.at(p-1).push_back(i);
        B.insert(p-1);
    }
    for(long long i=0; i<N; i++){
        cin >> p;
        Cc.at(p-1)++;
        C.insert(p-1);
    }
    set<long long> D;
    set_intersection(A.begin(), A.end(),B.begin(), B.end(),inserter(D, D.end()));
    long long ans = 0;
    for(auto itr = D.begin(); itr != D.end(); ++itr) {
        long long d = *itr;
        //cout << d;
        long long ccc=0;
        for(long long i=0; i < BB.at(d).size(); i++){
            long long bbb = BB.at(d).at(i);
            //cout << Ac.at(bbb) << "*" << Cc.at(bbb) << "=" << ans << endl;
            ccc += Cc.at(bbb);
        }
        ans += Ac.at(d) * ccc;
    }
    cout << ans;
}
#include                        <bits/stdc++.h>
#include                        <ext/pb_ds/assoc_container.hpp>
#define lb                      lower_bound
#define ub                      upper_bound
#define fe(x)                   (*(x.begin()))
#define le(x)                   (*(x.rbegin()))
#define pb                      push_back
#define load(node,n)            bool operator < (const node &n) const
#define fbo(x,id)               (*(x.find_by_order(id)))
#define ook(y)                  order_of_key(y)
#define all(x)                  (x).begin(),(x).end()
#define si(a)                   scanf("%d",&a)
#define sii(a,b)                scanf("%d %d",&a,&b)
#define siii(a,b,c)             scanf("%d %d %d",&a,&b,&c)
#define sl(a)                   scanf("%lld",&a)
#define sll(a,b)                scanf("%lld %lld",&a,&b)
#define slll(a,b,c)             scanf("%lld %lld %lld",&a,&b,&c)
#define rep(i,a,b)              for(ll i = a; i<=b;  i++)
#define un(x)                   x.erase(unique(all(x)), x.end())
#define TT                      long long TC;  scanf("%lld",&TC);  for(ll tc = 1; tc<=TC; tc++)
#define read                    freopen("input.txt","r",stdin)
#define write                   freopen("output.txt","w",stdout)
#define set(x)                  tree<x,null_type,less<x>,rb_tree_tag,tree_order_statistics_node_update>
#define assign(v,n,x)           v.clear(); for(int I = 0; I<n; I++)    v.pb(x);
#define SZ(x)                   (int)x.size()
#define ff                      first
#define ss                      second
#define FF                      fflush(stdout);
#define ms(AR,x)                memset(AR,x,sizeof AR);
#define case                    printf("Case %lld: ",tc);
using                           namespace __gnu_pbds;
using                           namespace std;
typedef                         long long ll;
typedef                         pair<ll,ll> pll;
typedef                         pair<int,int> pii;

#define mx 200003

ll ara[mx];

map<char,vector<ll> > ar;



main(){
//read;

int n;
si(n);

for(int i = 0; i<2*n; i++){
    ll x;
    sl(x);
    char c;
    cin>>c;
    ar[c].pb(x);
}

ll ans = 0;
vector<char> v;
char d;

for(auto i:ar){
    if( SZ(i.ss)%2 )  v.pb(i.ff),ans = 9e18;
    else d = i.ff;
}

for(char X:{'R','G','B'})
    sort(all(ar[X]));


if(SZ(v)){
    for(ll i:ar[v[0]]){
        int id = lb( all(ar[v[1]]),i ) - ar[v[1]].begin();
        id = min( id,SZ(ar[v[1]]) - 1 );
        int id2 = id - 1;
        id2 = max(0,id2);
        ll x = ar[v[1]][id],y = ar[v[1]][id2];
        ans = min( { ans, abs(i-x) , abs(i-y) } );
    }
    if(SZ(ar[d])){
        ll tmp[mx];
        set<pll> st;
        for(ll i = 0; i<SZ(ar[d]); i++){
            ll x = ar[d][i];
            int id = lb( all(ar[v[1]]), x ) - ar[v[1]].begin();
            id = min( id,SZ(ar[v[1]]) - 1 );
            int id2 = id - 1;
            id2 = max(0,id2);
            ll p = min( abs(x - ar[v[1]][id] ) , abs( x - ar[v[1]][id2]) );
            tmp[i] = p;
            st.insert(pll(p,i));
        }

        for(ll i = 0; i<SZ(ar[d]); i++){
            pll u = pll(tmp[i],i);
            st.erase(u);
            ara[i] = fe(st).ff;
            st.insert(u);
        }

        for(ll i = 0; i<SZ(ar[d]); i++){
            ll x = ar[d][i],y,z;
            int id = lb(all(ar[v[0]]),x) - ar[v[0]].begin();
            id = min( id, SZ(ar[v[0]]) - 1);
            int id2 = id - 1;
            id2 = max(0,id2);
            y = ar[v[0]][id];
            z = ar[v[0]][id2];
            ll p = min( abs(x - y), abs(x - z) );
            ans = min(ans, p + ara[i]);
        }
    }
}

cout<<ans;


return 0;
}

