#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>

using namespace std;

int main()
	{
	int N;
	cin >> N;
	N*=2;
	
	vector<pair<long long, int>> dogs;
	vector<int> color = vector<int>(3);
	color[0] = color[1] = color[2] = 0;
	int odd1=-1, odd2=-1;
	
	for(int i=0; i < N; i++)
		{
		long long x;	
		cin >> x;
		
		char c;
		cin >> c;
		while(c==' ')	
			cin >> c;
		
		switch(c)
			{
			case 'R':
				dogs.push_back(make_pair(x, 0));
				color[0]++;
				break;
			
			case 'G':
				dogs.push_back(make_pair(x, 1));
				color[1]++;
				break;
				
			case 'B':
				dogs.push_back(make_pair(x, 2));
				color[2]++;
				break;		
			}			 
		}
	
	for(int i=0; i < 3; i++)
		{
		if(color[i]%2==1)
			{
			if(odd1==-1)	odd1=i;
			else			odd2=i;
			}
		}
	
	if(odd1==-1)
		cout << 0 << endl;
	else
		{
		const long long INF = 1e16;  
		long long diff[3][3] = {{INF, INF, INF}, {INF, INF, INF},  {INF, INF, INF}};	
		sort(dogs.begin(), dogs.end());
		
		vector<long long> last = vector<long long>(3, -1);
		last[dogs[0].second] = dogs[0].first;
		for(int i=1; i < N; i++)	
			{	
			int tmp = dogs[i].second;
			last[tmp] = dogs[i].first; 
			for(int j=0; j < 3; j++)
				if(last[j]!=-1)
					diff[j][tmp] = diff[tmp][j] = min(diff[j][tmp], dogs[i].first-last[j]);
			}
			
		int odd3 = 3-odd1-odd2;	
		cout << min(diff[odd1][odd2], diff[odd1][odd3]+diff[odd3][odd2]) << endl;
		}
		
	return 0;
	}
#include <bits/stdc++.h>
#include <string>

typedef long long ll;
typedef long double ldb;
typedef double db;
using namespace std;

#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define ins insert
#define mp make_pair
#define len(s) s.length()
#define forp(i, a, b) for (ll i = a; i <= b; i++)
#define rep(i, n) for (ll i = 0; i < n; i++)
#define ren(i, n) for (ll i = n - 1; i >= 0; i--)
#define forn(i, a, b) for (ll i = a; i >= b; i--)
#define on cout << endl
#define o2(a, b) cout << a << " " << b
#define os cout << " "
#define all(v) v.begin(), v.end()
#define mem(n, m) memset(n, m, sizeof(n))
#define vi vector<int>
#define vl vector<ll>
#define ld long double
#define vld vector<ld>
#define vvi vector<vector<int>>
#define vvl vector<vector<long long>>
#define vvld vector<vector<ld>>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define mll map<ll, ll>
#define lb lower_bound
#define ub upper_bound
#define ret return 0
#define present(s, x) (s.find(x) != s.end())
#define cpresent(s, x) (find(all(s), x) != s.end())
#define ford(container, it) for (__typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define fors(container, it, a, b) for (__typeof(container.begin()) it = a; it != b; it++)
#define d1(x) cout << (x) << endl
#define d2(x, y) cout << (x) << " " << (y) << endl
#define d3(x, y, z) cout << (x) << " " << (y) << " " << (z) << endl
#define d4(a, b, c, d) cout << (a) << " " << (b) << " " << (c) << " " << (d) << endl
#define max3(a, b, c) max(max(a, b), c)
#define min3(a, b, c) min(min(a, b), c)
#define itoa(x) to_string(x);
#define atoi(x) stoll(x);

#define boost ios_base::sync_with_stdio(0)
#define MOD 1000000007
#define EPSILON 1e-9
#define PI 3.14159265358979323846
#define inf 999999999999999999
#define siz 100001
#define SIZ 1000001
#define SIZE 200001

#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define w(x)  \
    int x;    \
    cin >> x; \
    while (x--)

ll power(ll x, ll y, ll p)
{
    ll res = 1;
    x = x % p;
    while (y > 0)
    {
        if (y & 1)
            res = (res * x) % p;
        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}
ll pwr(ll x, ll y)
{
    ll res = 1;
    x = x;
    while (y > 0)
    {
        if (y & 1)
            res = (res * x);
        y = y >> 1;
        x = (x * x);
    }
    return res;
}
ll modInverse(ll n, ll p)
{
    return power(n, p - 2, p);
}
ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
ll lcm(ll a, ll b)
{
    return (b * a) / gcd(a, b);
}
ll fact(ll n)
{
    if (n <= 1)
        return 1;
    return n * fact(n - 1);
}
ll nPr(ll n, ll r)
{
    return fact(n) / fact(n - r);
}
ll cd(ll n)
{
    ll c=0;
    while(n>0)
    {
        n/=10;
        c++;
    }
    return c;
}
ll getIndex(vl v, ll K)
{
    auto it = find(v.begin(), v.end(), K);
 
    // If element was found
    if (it != v.end())
    {
     
        // calculating the index
        // of K
        ll index = it - v.begin();
        return index;
    }
    else {
        // If the element is not
        // present in the vector
        return -1;
    }
}
void c_p_c()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    c_p_c();
    ll t;
    t = 1;
    // cin >> t;
    while (t-- > 0)
    {      
        ll a, b;
        cin>>a>>b;
        ll d = b-a, j=d;
        for(j; j>(d+1)/2; j--)
        {
            if((j-a%j)%j<d-j+1)
                goto out;
        }
        out:
        d1(j);
    }
    return 0;
}