#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define fo(i, n) for (int i = 0; i < n; i++)
#define Fo(i, k, n) for (int i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1)
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<int, pair<int, int>> pipii;
typedef priority_queue<pii, vector<pii>, greater<pii>> minpq;
typedef priority_queue<int> maxpq;
//const double PI = acos(-1);
const int mod = 1000000007;

// ----------------------DEBUG TEMPLATE--------------------------
void __print(int x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V>
void __print(const pair<T, V> &x)
{
    cerr << '{';
    __print(x.first);
    cerr << ',';
    __print(x.second);
    cerr << '}';
}
template <typename T>
void __print(const T &x)
{
    int f = 0;
    cerr << '{';
    for (auto &i : x)
        cerr << (f++ ? "," : ""), __print(i);
    cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v)
{
    __print(t);
    if (sizeof...(v))
        cerr << ", ";
    _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)               \
    cerr << "[" << #x << "] = ["; \
    _print(x)
#else
#define debug(x...)
#endif
// =========================DEBUG TEMPLATE============================



void tc()
{
    int n,c;
    cin>>n>>c;

    vi a(n+1),b(n+1),cost(n+1);
    map<int,int>m;
    map<int,int>r;
    vi arr;
    fo(i,n){
        cin>>a[i+1]>>b[i+1]>>cost[i+1];
        arr.pb(a[i+1]);
        arr.pb(b[i+1]);
        arr.pb(b[i+1]+1);
    }
    sort(all(arr));
    int id = 1;
    fo(i,arr.size()){
        m[arr[i]] = id;
        r[id] = arr[i];
        id++;
    }
    id--;
    vi pref(id+1,0);
    fo(i,n){
        pref[m[a[i+1]]]+=cost[i+1];
        if(m[b[i+1]] +1 <=id){
            pref[m[b[i+1]] + 1] -= cost[i+1];
        }
    }

    for(int i =1;i<=id;i++){
        pref[i] += pref[i-1];
    }

    int ans = 0;

    Fo(i,1,id){
        
        int tod = r[i];
        int nxt = r[i+1];

        int dur = nxt - tod;
        
        ans += min(pref[i],c)*dur;
    }

    ans += min(c,pref[id]);

    cout<<ans<<endl;



}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);


    int t = 1;
    // cin >> t;
    while (t--)
    {
        tc();
    }
}

#include <bits/stdc++.h>
//#include<boost/multiprecision/cpp_int.hpp>
//#include<boost/multiprecision/cpp_dec_float.hpp>
//#include <atcoder/all>
using namespace std;
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define rrep(i, a) for (int i = (int)a; i > -1; --i)
#define REP(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define RREP(i, a, b) for (int i = (int)a; i > b; --i)
#define repl(i, a) for (ll i = (ll)0; i < (ll)a; ++i)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define popcount __builtin_popcount
#define popcountll __builtin_popcountll
#define fi first
#define se second
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll mod_998244353 = 998244353;
constexpr ll INF = 1LL << 60;

// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")

// using lll=boost::multiprecision::cpp_int;
// using
// Double=boost::multiprecision::number<boost::multiprecision::cpp_dec_float<128>>;//仮数部が1024桁
template <class T>
inline bool chmin(T &a, T b)
{
  if (a > b)
  {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmax(T &a, T b)
{
  if (a < b)
  {
    a = b;
    return true;
  }
  return false;
}

ll mypow(ll x, ll n, const ll &p = -1)
{ // x^nをmodで割った余り

  if (p != -1)
  {
    x = (x % p + p) % p;
  }
  ll ret = 1;
  while (n > 0)
  {
    if (n & 1)
    {
      if (p != -1)
        ret = (ret * x) % p;
      else
        ret *= x;
    }
    if (p != -1)
      x = (x * x) % p;
    else
      x *= x;
    n >>= 1;
  }
  return ret;
}

struct myrand{
  random_device seed;
  mt19937 mt;
  myrand():mt(seed()){}
  int operator()(int a,int b){//[a,b)
    uniform_int_distribution<int>dist(a,b-1);
    return dist(mt);
  }
};

// using namespace atcoder;

//------------------------
//------------------------
//------------------------
//------------------------
//------------------------

void solve()
{
  int n;
  cin>>n;
  multiset<ll>r,g,b;
  rep(i,n*2){
    ll a;
    char c;
    cin>>a>>c;
    if(c=='R')r.emplace(a);
    else if(c=='B')b.emplace(a);
    else g.emplace(a);
  }
  if(r.size()%2==0&&g.size()%2==0&&b.size()%2==0){
    cout<<0<<"\n";
  }else{
    if(r.size()%2==1){
      if(g.size()%2==0)swap(r,g);
      else swap(r,b);
    }
    ll ans=INF;
    ll G=INF,B=INF;
    for(auto x:g){
      auto itr=r.lower_bound(x);
      if(itr!=r.end())chmin(G,*itr-x);
      if(itr!=r.begin())chmin(G,x-*prev(itr));
    }
    for(auto x:b){
      auto itr=r.lower_bound(x);
      if(itr!=r.end())chmin(B,*itr-x);
      if(itr!=r.begin())chmin(B,x-*prev(itr));
    }
    chmin(ans,G+B);
    for(auto x:g){
      auto itr=b.lower_bound(x);
      if(itr!=b.end())chmin(ans,*itr-x);
      if(itr!=b.begin())chmin(ans,x-*prev(itr));
    }
    cout<<ans<<"\n";
  }
}

int main()
{
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(15);
  solve();
  return 0;
}