#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<cmath>
#include<queue>
#include<set>
#include<map>

using namespace std;

struct Dog {
	int pos;
	long long cute;
	int type;	//0--R, 1--G, 2--B
	bool isUsed;
};

std::vector<Dog> dog[3];
int r[3];
int n;
int totalN;
long long minDist;

void clearAll() {
	for (int i = 0 ; i < 3 ; i++) {
		for (int j = 0 ; j < dog[i].size() ; j++) {
			dog[i][j].isUsed = false;
		}
	}
}

bool compareDog(const Dog &a, const Dog &b) {
	return a.cute < b.cute;
}

long long check(int a, int b, int d) {
	std::vector<Dog> curDogs;
	
	for (int i = 0 ; i < dog[a].size() ; i++) {
		if (!dog[a][i].isUsed) {
			curDogs.push_back(dog[a][i]);
		}
	}
	for (int i = 0 ; i < dog[b].size() ; i++) {
		if (!dog[b][i].isUsed) {
			curDogs.push_back(dog[b][i]);
		}
	}
	sort(curDogs.begin(), curDogs.end(), compareDog);
	
	long long minDist = -1;
	int minPosA, minPosB;

	int startPos, endPos;
	if (d == 1) {
		startPos = 0;
		endPos = curDogs.size() - 1;
	} else {
		startPos = curDogs.size() - 2;
		endPos = -1;
	}
	
	for (int i = startPos ; i != endPos ; i = i + d) {
		if (curDogs[i].type != curDogs[i + 1].type) {
			long long curDist = abs(curDogs[i].cute - curDogs[i + 1].cute);
			if ((minDist == -1) || (minDist > curDist)) {
				minDist = curDist;
				
				if (curDogs[i].type == a) {
					minPosA = curDogs[i].pos;
					minPosB = curDogs[i + 1].pos;
				} else {
					minPosB = curDogs[i].pos;
					minPosA = curDogs[i + 1].pos;
				}
			}
		}
	}
	
	dog[a][minPosA].isUsed = true;
	dog[b][minPosB].isUsed = true;
	
	return minDist;
}




int main(void)
{
	scanf("%d", &n);
	totalN = n * 2;
	for (int i = 0 ; i < totalN ; i++) {
		long long cute;
		char ch;
		int type;
		scanf("%lld %c", &cute, &ch);
		
		if (ch == 'R') {
			type = 0;
		} else if (ch == 'G') {
			type = 1;
		} else if (ch == 'B') {
			type = 2;
		}
		
		Dog newDog;
		newDog.cute = cute;
		newDog.type = type;
		newDog.isUsed = false;
		dog[type].push_back(newDog);
	}
	for (int i = 0 ; i < 3 ; i++) {
		for (int j = 0 ; j < dog[i].size() ; j++) {
			dog[i][j].pos = j;
		}
	}
	
	int oddCount = 0;
	std::vector<int> oddPos; 
	int evenPos;
	for (int i = 0 ; i < 3 ; i++) {
		r[i] = dog[i].size() % 2;
		if (r[i] == 1) {
			oddCount++;
			oddPos.push_back(i);
		} else {
			evenPos = i;	//如果只有一个偶数，后面要用 
		}
	}
	
	if (oddCount == 0) {
		printf("0\n");
		return 0;
	}
	
	long long curMin;
	//先做下两个奇数的
	minDist = check(oddPos[0], oddPos[1], 1);
	clearAll();
	
	if (dog[evenPos].size() >= 2) {
		long long numA, numB;
		numA = check(oddPos[0], evenPos, 1);
		numB = check(oddPos[1], evenPos, 1);
		clearAll();
		curMin = numA + numB;
		if (minDist > curMin) minDist = curMin;
		
		numA = check(oddPos[1], evenPos, 1);
		numB = check(oddPos[0], evenPos, 1);
		clearAll();
		curMin = numA + numB;
		if (minDist > curMin) minDist = curMin;
		
		numA = check(oddPos[0], evenPos, -1);
		numB = check(oddPos[1], evenPos, -1);
		clearAll();
		curMin = numA + numB;
		if (minDist > curMin) minDist = curMin;
		
		numA = check(oddPos[1], evenPos, -1);
		numB = check(oddPos[0], evenPos, -1);
		clearAll();
		curMin = numA + numB;
		if (minDist > curMin) minDist = curMin;
	}
	
	
	
	printf("%lld\n", minDist);
	
	return 0;
}
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>


#define rep(i,n) for(int i =0;i<(n);++i)

using namespace std;
using ll = long long;

struct DataType {
    ll val;
    char col;
};
constexpr ll ll_max {1001001001001001};

int main(int argc, char** argv) {
    int n; cin >> n;

    std::vector<vector<ll>> a(3);

    rep(i,2*n) {
        DataType tmp;
        cin >> tmp.val >> tmp.col;
        if (false);
        else if (tmp.col == 'R') a[0].push_back(tmp.val);
        else if (tmp.col == 'G') a[1].push_back(tmp.val);
        else if (tmp.col == 'B') a[2].push_back(tmp.val);
    }

    std::sort(a[0].begin(), a[0].end());
    std::sort(a[1].begin(), a[1].end());
    std::sort(a[2].begin(), a[2].end());

    int a_mod = a[0].size() % 2;
    int b_mod = a[1].size() % 2;
    int c_mod = a[2].size() % 2;
    if (a_mod == 0 && b_mod == 0 && c_mod == 0) {
        cout << "0\n";
    } else {
        int idx_1, idx_2;
        if (a_mod == 1) idx_1 = 0;
        else if (b_mod == 1) idx_1 = 1;
        if (c_mod == 1) idx_2 = 2;
        else if (b_mod == 1) idx_2 = 1;
        int idx_3 = 3 - idx_1 - idx_2;

        ll min_val {ll_max};

        rep(i, a[idx_1].size()) {
            auto it = std::lower_bound(a[idx_2].begin(), a[idx_2].end(), a[idx_1][i]);
            if (it != a[idx_2].end()) min_val = std::min(min_val, *it - a[idx_1][i]);
            if (it != a[idx_2].begin()) min_val = std::min(min_val, std::abs(*(--it) - a[idx_1][i]));
        }

        ll min_val_tmp1 {ll_max};

        rep(i, a[idx_1].size()) {
            auto it = std::lower_bound(a[idx_3].begin(), a[idx_3].end(), a[idx_1][i]);
            if (it != a[idx_3].end()) min_val_tmp1 = std::min(min_val_tmp1, *it - a[idx_1][i]);
            if (it != a[idx_3].begin()) min_val_tmp1 = std::min(min_val_tmp1, std::abs(*(--it) - a[idx_1][i]));
        }
        ll min_val_tmp2 {ll_max};
        rep(i,a[idx_2].size()) {
            auto it = std::lower_bound(a[idx_3].begin(), a[idx_3].end(), a[idx_2][i]);
            if (it != a[idx_3].end()) min_val_tmp2 = std::min(min_val_tmp2, *it - a[idx_2][i]);
            if (it != a[idx_3].begin()) min_val_tmp2 = std::min(min_val_tmp2, std::abs(*(--it) - a[idx_2][i]));
        }

        cout << std::min(min_val, min_val_tmp1 + min_val_tmp2) << '\n';
    }
}
