// Problem: B - Special Subsets
// Contest: AtCoder - AtCoder Regular Contest 114
// URL: https://atcoder.jp/contests/arc114/tasks/arc114_b
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)


// By AmmarDab3an - Aleppo University

#include "bits/stdc++.h"

using namespace std;

//#define int int64_t
//#define lli int64_t

typedef unsigned int        uint;
typedef long long int       lli;
typedef unsigned long long  ull;
typedef pair<int, int>      pii;
typedef pair<lli, lli>      pll;
typedef pair<int, pii>      iii;
typedef pair<lli, pll>      lll;
typedef vector<int>         vi;
typedef vector<lli>         vl;
typedef vector<pii>         vpii;
typedef vector<pll>         vpll;

#define endl '\n'
#define fastIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define freopenI freopen("input.txt", "r", stdin);
#define freopenO freopen("output.txt", "w", stdout);

const int INF = 0x3f3f3f3f;
const lli INFLL = 0x3f3f3f3f3f3f3f3f;
const int MOD = 998244353;
const double EPS = 1e-9;
const double  PI = acos(-1);

const int  MAX = 2e5 + 10;
const int NMAX = 2e5 + 10;
const int MMAX = 2e5 + 10;

inline int add(int a, int b){
    return ((lli) a + b + MOD + MOD) % MOD;
}

inline int mul(int a, int b){
    return ((lli) a * b) % MOD;
}

int fast_pow(int n, int p){
    if(!p) return 1;
    if(p&1) return mul(n, fast_pow(n, p-1));
    int tmp = fast_pow(n, p/2);
    return mul(tmp, tmp);
}

int32_t main(){
    
    fastIO;
    
#ifdef LOCAL
    freopenI;
    freopenO;
#endif

    int n;
    cin >> n;
    
    vi vec(n);
    for(auto &i : vec) cin >> i;
    for(auto &i : vec) i--;
    
    vi vis(n);
    int vid = 1;
    
    int cnt = 0;
    
    for(int i = 0; i < n; i++) if(!vis[i]){
        
        cnt++;
        vid++;
        
        int t = i;
        while(true){
            
            if(vis[t] == vid) break;
            
            if(vis[t]){
                cnt--;
                break;
            }
            
            vis[t] = vid;
            t = vec[t];
        }
        
    }
    
    cout << add(fast_pow(2, cnt), -1) << endl;
}

#pragma GCC optimize("O3")   //
#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define per(i, b, a) for(int i = b - 1; i >= a; i--)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef long double ld;
typedef unsigned long long ull;

unsigned seed = chrono::system_clock::now().time_since_epoch().count();
mt19937 eng(seed);

ll random2(){
    return (1ll << 31ll)*eng()+eng();
}

ll n,m,k,q,T;

const ll big = 1000000007;
const ll big2 = 1000000009;
const ll mod =  998244353;

const int MAXN = 400001;

vector<int> prio = {0,1,2};
vector<vl> A(3, vl());

ll score(int i, int j){
    vector<pll> hej;
    trav(a, A[i]){
        hej.push_back({a, 0});
    }
    trav(a, A[j]){
        hej.push_back({a, 1});
    }
    sort(all(hej));
    ll ans = big*big;
    rep(c1,0,sz(hej)-1){
        if(hej[c1].second != hej[c1+1].second){
            ans = min(ans, hej[c1+1].first-hej[c1].first);
        }
    }
    return ans;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    //freopen("fhc.txt","r",stdin);
    //freopen("autput.txt","w",stdout);

    ll a,b,c,d,e;

    cin >> n;
    rep(c1,0,2*n){
        char ch;
        cin >> a >> ch;
        b = 0;
        if(ch == 'G')b = 1;
        if(ch == 'B')b = 2;
        A[b].push_back(a);
    }

    ll ans = big*big;

    rep(_,0,6){
        ll F[3] = {0,0,0};
        rep(c1,0,3){
            F[c1] = sz(A[prio[c1]]);
        }
        ll temp = 0;
        rep(c1,0,2){
            if(F[c1]%2 == 1){
                temp += score(prio[c1], prio[c1+1]);
                F[c1+1]--;
            }
        }
        ans = min(ans, temp);
        next_permutation(all(prio));
    }
    cout << ans << "\n";



    return 0;
}
