#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define R cin>>
#define ll long long
#define ln cout<<'\n'
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n",a)
#define mem(a) memset(a,0,sizeof(a))
#define all(c) (c).begin(),(c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i,n) for(ll i=(ll)(n)-1;i>=0;i--)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define rep(i,n) REP(i,0,n)
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}void pr(){ln;}
template<class A,class...B>void pr(const A &a,const B&...b){cout<<a<<(sizeof...(b)?" ":"");pr(b...);}
template<class A>void PR(A a,ll n){rep(i,n)cout<<(i?" ":"")<<a[i];ln;}
const ll MAX=1e9+7,MAXL=1LL<<61,dx[8]={-1,0,1,0,-1,-1,1,1},dy[8]={0,1,0,-1,-1,1,1,-1};
typedef pair<ll,ll> P;

void Main() {
  ll n;
  R n;
  n*=2;
  vector<ll> a[3];
  rep(i,n) {
    ll x;
    char c;
    cin >> x >> c;
    if(c=='R') a[0].pb(x);
    else if(c=='B') a[1].pb(x);
    else a[2].pb(x);
  }
  rep(i,3) sort(all(a[i]));
  P c[3];
  rep(i,3) c[i]=P(a[i].size()%2,i);
  sort(c,c+3,greater<P>());
  ll ans=MAXL;
  if(c[0].F) {
    ll x=c[0].S,y=c[1].S,z=c[2].S;
    rep(i,a[x].size()) {
      ll k=lower_bound(all(a[y]),a[x][i])-a[y].begin();
      if(k<a[y].size()) ans=min(ans,abs(a[x][i]-a[y][k]));
      if(k) ans=min(ans,abs(a[x][i]-a[y][k-1]));
    }
    vector<P> v[2];
    rep(i,a[z].size()) {
      ll k=lower_bound(all(a[x]),a[z][i])-a[x].begin();
      if(k<a[x].size()) {
        ll d=abs(a[z][i]-a[x][k]);
        v[0].pb(P(d,i));
      }
      if(k) {
        ll d=abs(a[z][i]-a[x][k-1]);
        v[0].pb(P(d,i));
      }
    }
    rep(i,a[z].size()) {
      ll k=lower_bound(all(a[y]),a[z][i])-a[y].begin();
      if(k<a[y].size()) {
        ll d=abs(a[z][i]-a[y][k]);
        v[1].pb(P(d,i));
      }
      if(k) {
        ll d=abs(a[z][i]-a[y][k-1]);
        v[1].pb(P(d,i));
      }
    }
    rep(i,2) sort(all(v[i]));
    rep(i,min(3,(int)v[0].size())) {
      rep(j,min(3,(int)v[1].size())) {
        if(v[0][i].S!=v[1][j].S) ans=min(ans,v[0][i].F+v[1][i].F);
      }
    }
  } else ans=0;
  pr(ans);
}

int main(){ios::sync_with_stdio(0);cin.tie(0);Main();return 0;}

#include <bits/stdc++.h>
using namespace std;

//.define
// #define FILE_IN_OUT
#define RET(_x)    cout << (_x) << '\n'; return;
#define all(_obj)  (_obj).begin(), (_obj).end()
#define loop(_n)   for (int i = 0; i < (_n); ++i)
#define sz(_obj)   static_cast<int>((_obj).size())
#ifdef SHJ_LOCAL
#define debug(_x)  std::cerr << (#_x) << " = " << (_x) << '\n' << std::flush;
#else
#define debug(_x)  {}
#endif
#define endl "\n"
template<typename P, typename Q>
inline P CeilDiv(P _dividend, Q _divider) {
    return static_cast<P>((_dividend - 1LL + _divider) / _divider);
}
template<typename Tp>
inline void outarr(Tp _begin, Tp _end, const char* _delim = " ") {
    for (Tp current = _begin; current != _end; ++current) {
        std::cout << *current << _delim;
    }
    std::cout << '\n';
}

//.constant
using ll = int64_t;
using pii = std::pair<int, int>;
constexpr int INF = 0x3f3f3f3f;
constexpr int MOD = static_cast<int>(1e9 + 7);

//.data
const vector<int> primes{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71};

//.code
void SolveTask() {
    ll a, b;
    cin >> a >> b;
    vector<int> arr(b - a + 1, 0);
    for (ll i = a; i <= b; ++i) {
        for (int j = 0; j < sz(primes); ++j) {
            if (i % primes[j] == 0) {
                arr[i - a] |= 1 << j;
            }
        }
    }
    const int full_mask = 1 << sz(primes);
    vector<ll> dp(full_mask, 0);
    dp[0] = 1;
    loop(sz(arr)) {
        for (int mask = 0; mask < full_mask; ++mask) {
            if ((mask & arr[i]) == 0) {
                dp[mask | arr[i]] += dp[mask];
            }
        }
    }
    cout << accumulate(all(dp), 0LL) << endl;
}

int main() {
#ifdef FILE_IN_OUT
    std::ifstream cin ("input.txt",  std::ios::in);
    std::ofstream cout("output.txt", std::ios::out);
#else
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
#endif
    int test_cases = 1;
    // cin >> test_cases;
    for (int yt = 1; yt <= test_cases; ++yt) {
        SolveTask();
    }
#ifdef FILE_IN_OUT
    cin.close();
    cout.close();
#endif
    return 0;
}