#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int,int> pii;
typedef pair<double,double> pdd;
const int N=2e5+5;
const int M=1e5+5;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
const double eps=1e-8;
const long double pi=acos(-1.0L);
#define ls (i<<1)
#define rs (i<<1|1)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define mk make_pair
#define mem(a,b) memset(a,b,sizeof(a))
LL read()
{
    LL x=0,t=1;
    char ch;
    while((ch=getchar())<'0'||ch>'9') if(ch=='-') t=-1;
    while(ch>='0'&&ch<='9'){ x=(x<<3)+(x<<1)+ch-'0'; ch=getchar(); }
    return x*t;
}
vector<LL> a[3];
vector<pair<LL,int> >c;

int id(char ch)
{
    if(ch=='R') return 0;
    if(ch=='G') return 1;
    return 2;
}
int main()
{
    int n=read();
    for(int i=1;i<=2*n;i++)
    {
        LL x=read();
        int s[2];
        scanf("%s",s);
        a[id(s[0])].eb(x);
    }
    int tot=0;
    for(int i=0;i<3;i++)
    {
        if((int)a[i].size()&1) {
            for(auto x:a[i]) c.eb(x,tot);
            tot++;
        }
    }
    if(c.empty()) return 0*printf("0\n");
    sort(c.begin(),c.end());
    LL ans=1e16;
    for(int i=1;i<(int)c.size();i++)
        if(c[i].se!=c[i-1].se) ans=min(ans,c[i].fi-c[i-1].fi);
    for(int i=0;i<3;i++)
    {
        if((int)a[i].size()&1) continue;
        for(auto x:a[i]) c.eb(x,tot);
    }
    sort(c.begin(),c.end());
    LL t1=1e16,t2=1e16;

    for(int i=0;i<(int)c.size();i++)
        if(c[i].se==2) {
            if(i>0&&c[i-1].se==0) t1=min(t1,c[i].fi-c[i-1].fi);
            if(i<(int)c.size()-1&&c[i+1].se==0) t1=min(t1,c[i+1].fi-c[i].fi);
            if(i>0&&c[i-1].se==1) t2=min(t2,c[i].fi-c[i-1].fi);
            if(i<(int)c.size()-1&&c[i+1].se==1) t2=min(t2,c[i+1].fi-c[i].fi);
        }
   // printf("%lld %lld\n",t1,t2);
    printf("%lld\n",min(ans,t1+t2));
    return 0;
}
/*
2
100 R
2 B
99 G
1 G
*/

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define cinf(n,x) for(int i=0;i<(n);i++)cin>>x[i];
#define ft first
#define sc second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(),(v).end()
#define LB(a,x) lb(all(a),x)-a.begin()
#define UB(a,x) ub(all(a),x)-a.begin()
//#define mod 1000000007
#define mod 998244353
#define FS fixed<<setprecision(15)
using namespace std;
typedef long long ll;
const double pi=3.141592653589793;
template<class T> using V=vector<T>;
using P=pair<ll,ll>;
typedef unsigned long long ull;
typedef long double ldouble;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline void out(T a){ cout << a << '\n'; }
void YN(bool ok){if(ok) cout << "Yes" << endl; else cout << "No" << endl;}
//void YN(bool ok){if(ok) cout << "YES" << endl; else cout << "NO" << endl;}


const ll INF=1e18;
const int mx=200005;
//arc114
ll dp[200005][4];
class UnionFind{
public:
    vector<ll> par,num;

    int find(ll v){
        return (par[v]==v)? v: (par[v]=find(par[v]));
    }
    explicit UnionFind(ll N):par(N),num(N,1){
        iota(par.begin(),par.end(),0);
    }
    void unite(ll u,ll v){
        u=find(u),v=find(v);
        if(u==v)return ;
        if(num[u]<num[v])swap(u,v);
        num[u] += num[v];
        par[v] = u;
    }
    bool same(ll u,ll v){
        return find(u) == find(v);
    }
    bool ispar(ll v){
        return v=find(v);
    }
    ll size(ll v){
        return num[find(v)];
    }
};

int main(){
    //オーバーフローは大丈夫ですか？？
    cin.tie(0);ios::sync_with_stdio(false);
    ll n;
    cin>>n;
    V<ll> a(n);
    V<V<ll>> G(n);
    UnionFind uf(n);
    rep(i,n){
        cin>>a[i];
        a[i]--;
        uf.unite(i,a[i]);
    }
    set<ll> st;
    rep(i,n) st.insert(uf.find(i));
    ll x=st.size();
    ll ans=1;
    rep(i,x) ans=ans*2%mod;
    ans--;
    if(ans<0) ans+=mod;
    out(ans);
}
//ペナルティ出しても焦らない　ACできると信じろ！！！
//どうしてもわからないときはサンプルで実験　何か見えてくるかも
//頭で考えてダメなら紙におこせ！！