#define _USE_MATH_DEFINES
#include<iostream>
#include<iomanip>
#include<vector>
#include<string>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<climits>
#include<bitset>
#include<unordered_map>
#include<unordered_set>
#include<random>
#include<list>
#include<functional>


using namespace std;

#define MAX(A,B) ((A)>(B)?(A):(B))
#define MIN(A,B) ((A)<(B)?(A):(B))
#define LP(I,S,G) for (long long int I = S; I < G; I++)
#define IN(X) 	for (int i = 0; i < X.size(); i++)cin >> X[i]
#define OUT(X) 	for (int i = 0; i < X.size(); i++)cout << X[i]<<" "
#define SORT(X) sort(X.begin(), X.end())
#define CSORT(X,Y) sort(X.begin(), X.end(),Y)
#define COPY(X,Y) copy(X.begin(), X.end(), Y.begin())
#define ALL(X,Y) for (auto X = Y.begin(); X != Y.end(); X++)
#define FULL(a)  (a).begin(),(a).end()

template<class I1, class I2>
istream& operator>>(istream& s, pair<I1, I2>& in) {
	s >> in.first >> in.second;
	return s;
}

long long int M = pow(10, 9) + 7;

uint64_t combinations2(uint64_t n, uint64_t k) {
	uint64_t r = 1;
	for (uint64_t d = 1; d <= k; ++d) {
		r *= n--;
		r /= d;
	}
	return r;
}

struct HashPair {

	//注意 constがいる
	template<class T1, class T2>
	size_t operator()(const pair<T1, T2> &p) const {

		//first分をハッシュ化する
		auto hash1 = hash<T1>{}(p.first);

		//second分をハッシュ化する
		auto hash2 = hash<T2>{}(p.second);

		//重複しないようにハッシュ処理
		size_t seed = 0;
		seed ^= hash1 + 0x9e3779b9 + (seed << 6) + (seed >> 2);
		seed ^= hash2 + 0x9e3779b9 + (seed << 6) + (seed >> 2);
		return seed;
	}
};

int main(void) {
	int n;
	cin >> n;
	vector<long long int> r, g, b;
	LP(i, 0, 2*n) {
		long long int a;
		char c;
		cin >> a >> c;
		switch (c){
		case 'R':
			r.push_back(a);
			break;
		case 'G':
			g.push_back(a);
			break;
		case 'B':
			b.push_back(a);
			break;
		default:
			break;
		}
	}
	vector<long long int> x, y,z;
	if (r.size() % 2 == 0 && g.size() % 2 == 0 && b.size() % 2 == 0) {
		cout << 0;
		return 0;
	}
	else if (r.size() % 2 == 0) {
		x = g, y = b,z = r;
	}
	else if (g.size() % 2 == 0) {
		x = r, y = b, z = g;
	}
	else x = r, y = g, z = b;
	SORT(x);
	SORT(y);
	SORT(z);
	long long int min = LLONG_MAX;
	ALL(i, x) {
		auto key = lower_bound(FULL(y), *i);
		if(key!=y.end())min = MIN(min, abs(*key - *i));
		if (key != y.begin()) {
			min = MIN(min, abs(*i - *(key - 1)));
		}
	}
	if (z.size() != 0) {
		long long int min_x = LLONG_MAX, min_y = LLONG_MAX;
		ALL(i, x) {
			auto key = lower_bound(FULL(z), *i);
			if (key != z.end())min_x = MIN(min_x, abs(*key - *i));
			if (key != z.begin()) {
				min_x = MIN(min_x, abs(*i - *(key - 1)));
			}
		}
		ALL(i, y) {
			auto key = lower_bound(FULL(z), *i);
			if (key != z.end())min_y = MIN(min_y, abs(*key - *i));
			if (key != z.begin()) {
				min_y = MIN(min_y, abs(*i - *(key - 1)));
			}
		}
		min = MIN(min, min_x + min_y);

	}
	cout << min;
	return 0;
}
#include<bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define eb emplace_back
#define all(a) (a).begin(),(a).end()
#define SZ(a) (int)(a).size()
#define FOR(i, a, b) for(int i=(a); i<=(b); ++i)
#define ROF(i, a, b) for(int i=(a); i>=(b); --i)
#define make_unique(a) sort(all((a))), (a).resize(unique(all((a)))-(a).begin())
#define pc(x) putchar(x)
#define make_pair MP
#define make_tuple MT

using namespace std;

typedef long long i64;
typedef tuple<int,int,int> iii;
typedef pair<int,int> pii;
typedef pair<long,long> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;

i64 s[2][60];
int dp[60][2][2]; // normal, or reverse

void solve(){
    i64 A, B;
    cin >> A >> B;
    if(A > B){
        printf("%lld",A-B); return;
    }
    i64 ans = B-A;
    int bit = 0;
    do{
        A *= 2;
        bit++;
        i64 res = 0, res2 = 0;
        i64 diff = abs(B-A);
        res += (diff>>bit);
        res2 = res+1;
        diff &= ((1ll<<bit)-1);
        i64 diff2 = (1ll<<bit)-diff;
        int last = -1;
        FOR(i, 0, bit){
            if(diff&(1ll<<i)){
                if(last == -1){
                    last = i;
                }
            }else{
                if(last != -1){
                    res += min(i-last, 2);
                    last = -1;
                }
            }
        }
        last = -1;
        FOR(i, 0, bit){
            if(diff2&(1ll<<i)){
                if(last == -1){
                    last = i;
                }
            }else{
                if(last != -1){
                    res2 += min(i-last, 2);
                    last = -1;
                }
            }
        }
        //printf("%d %lld %lld\n",bit,res,res2);
        ans = min(ans, bit+min(res,res2));
    }while(A < B);
    printf("%lld",ans);
}

int main(){
	cin.tie(0)->sync_with_stdio(false);
	cout.tie(0);
    solve();
	return 0;
}
/*
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 */