#include<bits/stdc++.h>
using ll = int_fast64_t;
using P = std::pair<ll,ll>;
using PP = std::pair<ll,P>;

#define REP(i,b,e) for(int i=b; i<e; i++)
#define PRINT(vec) {printf("[ ");for(auto &i:vec)printf("%ld ",i);puts("]");}
#define fi first
#define se second

const int MOD = 1e9+7;
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1}, dy[] = {1, 0, -1, 0, 1, -1, -1, 1};

int main(){
	int n;
	scanf("%d", &n);
	int a[n];
	REP(i, 0, n) scanf("%d", &a[i]);

	int last = -1;
	REP(i, 0, n){
		if(i>a[i]-1){
			if(a[i]-1<last){
				puts("-1");
				return 0;
			}
			last = i;
		}
	}
	last = n;
	for(int i=n-1; i>=0; i--){
		if(i<a[i]-1){
			if(a[i]-1>last){
				puts("-1");
				return 0;
			}
			last = i;
		}
	}

	std::set<int> used;
	std::vector<int> ans;
	int pos[n];
	REP(i, 0, n) pos[a[i]-1] = i;
	REP(i, 0, n){
		if(i<pos[i]){
			for(int j=pos[i]; j>i; j--){
				if(used.count(j)){
					puts("-1");
					return 0;
				}
				used.insert(j);
				ans.push_back(j);
				pos[a[j-1]-1]++;
				std::swap(a[j-1], a[j]);
			}
			pos[i] = i;
		}else{
			for(int j=pos[i]; j<i; j++){
				if(used.count(j+1)){
					puts("-1");
					return 0;
				}
				used.insert(j+1);
				ans.push_back(j+1);
				pos[a[j+1]]--;
				std::swap(a[j], a[j+1]);
			}
		}
	}
	if(used.size()!=n-1) puts("-1");
	else for(int x: ans) printf("%d\n", x);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define fo(i,a,b) for(int i = a; i<b ; i++)
#define FIO ios_base::sync_with_stdio(false);   cin.tie(NULL);   cout.tie(NULL);
#define pb push_back
#define M 1000000007
int mod(int x){
return ((x%M + M)%M);
}
int add(int a,int b){
return mod(mod(a)+mod(b));
}
int mul(int a,int b){
return mod(mod(a)*mod(b));
}
// ****************************************************************************
int fun2(vector<int>&a,multiset<int>&st){
    if(st.size()==0)
    return 1e16;
    int val;
    int ans = LLONG_MAX;
    fo(i,0,a.size()){
        auto it = st.lower_bound(a[i]);
        if(it!=st.begin())
        it--;
        fo(k,0,3){
            if(it==st.end())
            break;
            if(ans>(abs(*it-a[i]))){
                ans=abs(*it-a[i]);
                val=*it;
            }
            it++;
        }
    }
    st.erase(st.find(val));
    return ans;
}
int fun(vector<int>&a,vector<int>&b,vector<int>&c){
    sort(a.begin(),a.end());
    sort(b.begin(),b.end());
    int ans = LLONG_MAX;
    fo(i,0,a.size()){
        auto it = lower_bound(b.begin(),b.end(),a[i]);
        if(it!=b.begin())
        it--;
        fo(k,0,3){
            if(it==b.end())
            break;
            ans=min(ans,abs(*it-a[i]));
            it++;
        }
    }
    multiset<int>st;
    for(auto it : c)
    st.insert(it);
    if(st.size()==0)
    return ans;
   int tempans = fun2(a,st);
   tempans+=fun2(b,st);
    return min(ans,tempans);
}
void solve(){
    int n;
    cin>>n;
    n*=2;
    vector<int>r,g,b;
    fo(i,0,n){
        int x;
        cin>>x;
        char cc;
        cin>>cc;
        if(cc=='R'){
            r.pb(x);
        }
        else if(cc=='G')
        g.pb(x);
        else b.pb(x);        
    }
    if(r.size()%2==0 && g.size()%2==0 && b.size()%2==0){
        cout<<"0";
        return;
    }   
    if(r.size()%2 && g.size()%2)
    cout<<fun(r,g,b);
    else if(r.size()%2 && b.size()%2)
    cout<<fun(r,b,g);
    else cout<<fun(g,b,r);
}   
signed main()
{
FIO
int t;
t=1;
// cin>>t;
while(t--)
{
solve();
}
}