#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define eb emplace_back
#define x first
#define y second
#define FOR(i, m, n) for (ll i(m); i < n; i++)
#define DWN(i, m, n) for (ll i(m); i >= n; i--)
#define REP(i, n) FOR(i, 0, n)
#define DW(i, n) DWN(i, n, 0)
#define F(n) REP(i, n)
#define FF(n) REP(j, n)
#define D(n) DW(i, n)
#define DD(n) DW(j, n)
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
using tll = tuple<ll, ll, ll>;
using vll = vector<ll>;
using vpll = vector<pll>;
using vtll = vector<tll>;
using gr = vector<vll>;
using wgr = vector<vpll>;
void add_edge(gr&g,ll x, ll y){ g[x].pb(y);g[y].pb(x); }
void add_edge(wgr&g,ll x, ll y, ll z){ g[x].eb(y,z);g[y].eb(x,z); }
template<typename T,typename U>
ostream& operator<<(ostream& os, const pair<T,U>& p) { 
    cerr << ' ' <<  p.x << ',' << p.y; return os; }
template <typename T> 
ostream& operator<<(ostream& os, const vector<T>& v) { 
    for(auto x: v) os << ' ' <<  x; return os; }
template <typename T> 
ostream& operator<<(ostream& os, const set<T>& v) { 
    for(auto x: v) os << ' ' <<  x; return os; }
template<typename T,typename U>
ostream& operator<<(ostream& os, const map<T,U>& v) { 
    for(auto x: v) os << ' ' <<  x; return os; }
struct d_ {
    template<typename T> d_& operator,(const T& x) {
        cerr << ' ' <<  x; return *this;}
} d_t;
#define dbg(args ...) { d_t,"|",__LINE__,"|",":",args,"\n"; }
#define deb(X ...) dbg(#X, "=", X);
#define EPS (1e-10)
#define INF (1LL<<61)
#define CL(A,I) (memset(A,I,sizeof(A)))
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()

ll n;
gr g;

pll rec(ll u) { // ret (what takes Taker, odd/even)
    ll ret = 1, cnt = 1;
    vector<vll> o,e;
    for(auto v: g[u]) {
        auto [val, sz] = rec(v);
        if(sz&1) o.pb({2*val-sz, val,sz-val});
        else e.pb({2*val-sz, val,sz-val});
        cnt += sz;
    }
    sort(all(o));
    F(o.size()) ret += o[i][1+(i&1)];
    F(e.size()) if(e[i][0]<=0) {
        ret += e[i][1];
    } else {
        ret += e[i][1+(o.size()&1)];
    }
    return {ret, cnt};
}

int main(void) {
    ios_base::sync_with_stdio(false);
    cin >> n;
    g = gr(n);
    F(n-1) {
        ll x; cin >> x; x--;
        g[x].pb(i+1);
    }
    cout << rec(0).x << endl;
    return 0;
}

#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
using ll=long long;
using ld=long double;
using pll=pair<ll, ll>;
//using mint = modint1000000007;
#define rep(i,n) for (ll i=0; i<n; ++i)
#define all(c) begin(c),end(c)
#define PI acos(-1)
#define oo 2e18
template<typename T1, typename T2>
bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;}
template<typename T1, typename T2>
bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;}
vector<pll> RG;
vector<pll> GB;
vector<pll> RB;
/*

*/
ll A[200010];
char C[200010];
vector<pll> P(0);
ll N, r, g, b, dog;
ll ans = oo;
bool used[200010] = {};

void dfs(ll depth, ll pre, ll score)
{
  if (r%2 == 0 && g%2 == 0 && b%2 == 0){
    chmin(ans, score);
    return ;
  }
  if (depth == 2) return ;
  ll k, s;
  rep(i, 3){
    if (i==pre) continue;
    if (i==0){//okになるid探す
      ll sz = RG.size();
      bool ok = false;
      rep(j, sz){
        k=RG[j].second;
        s=RG[j].first;
        if (used[k] || used[k+1]) continue;
        else{
          ok = true;
          break;
        }
      }
      if (!ok) continue;
      r--, g--;
    }
    if (i==1){//okになるid探す
      ll sz = GB.size();
      bool ok = false;
      rep(j, sz){
        k=GB[j].second;
        s=GB[j].first;
        if (used[k] || used[k+1]) continue;
        else{
          ok = true;
          break;
        }
      }
      if (!ok) continue;
      g--, b--;
    }
    if (i==2){//okになるid探す
      ll sz = RB.size();
      bool ok = false;
      rep(j, sz){
        k=RB[j].second;
        s=RB[j].first;
        if (used[k] || used[k+1]) continue;
        else{
          ok = true;
          break;
        }
      }
      if (!ok) continue;
      r--, b--;
    }
    used[k] = true;
    used[k+1] = true;
    dfs(depth+1, i, score + s);
    // 復元
    if (i==0) r++, g++;
    if (i==1) g++, b++;
    if (i==2) r++, b++;
    used[k] = false;
    used[k+1] = false;
  }
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout << fixed << setprecision(10);
  cin >> N;
  dog = N*2;
  rep(i, dog){
    ll a;
    char c;
    cin >> a >> c;
    A[i] = a;
    C[i] = c;
    if (c == 'R') r++;
    if (c == 'G') g++;
    if (c == 'B') b++;
    P.push_back({a, c});
  }
  if (r%2 == 0 && g%2 == 0 && b%2 == 0)
  {
    cout << 0 << endl;
    return 0;
  }
  sort(all(P));
  rep(i, dog-1){
    ll pr=0, pg=0, pb=0;
    if (P[i].second == P[i+1].second) continue;
    if (P[i].second == 'R') pr++;
    if (P[i].second == 'G') pg++;
    if (P[i].second == 'B') pb++;
    if (P[i+1].second == 'R') pr++;
    if (P[i+1].second == 'G') pg++;
    if (P[i+1].second == 'B') pb++;
    ll dif = P[i+1].first - P[i].first;
    if (pr && pg) RG.push_back({dif, i});
    if (pg && pb) GB.push_back({dif, i});
    if (pr && pb) RB.push_back({dif, i});
  }
  sort(all(RG));
  sort(all(GB));
  sort(all(RB));
  dfs(0, -1, 0);
  cout << ans << endl;
}