#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
/*#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")*/

using namespace __gnu_pbds;
using namespace std;

#define ll long long
#define ld long double
#define mp make_pair
 
typedef long double DOUBLE;
typedef complex<DOUBLE> COMPLEX;
typedef vector<DOUBLE> VD;
typedef vector<COMPLEX> VC;

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

 
void print(auto &a)
{
    for (auto s: a) cout<<s<<' ';
    cout<<endl;
}
 
void get(vector<auto> &a)
{
    for (int i = 0; i<(int)a.size(); i++) cin>>a[i];
}
 
const int p = 998244353;
 
 
int mul(int a, int b) {
    return (1LL * a%p * b%p) % p;
}
 
int add(int a, int b) {
    int s = (a+b);
    s = s%p;
    if (s<0) s+=p; 
    return s;
}
 
int sub(int a, int b) {
    int s = (a-b);
    s = s%p;
    if (s<0) s+=p; 
    return s;
}
 
int po(int a, int deg)
{
    if (deg==0) return 1;
    if (deg%2==1) return mul(a, po(a, deg-1));
    int t = po(a, deg/2);
    return mul(t, t);
}
 
int inv(int n)
{
    return po(n, p-2);
}
 
vector<int> Z(vector<int> s)
{
int n = s.size();
vector<int> z(n);
int L = 0, R = 0;
for (int i = 1; i < n; i++) {
  if (i > R) {
    L = R = i;
    while (R < n && s[R-L] == s[R]) R++;
    z[i] = R-L; R--;
  } else {
    int k = i-L;
    if (z[k] < R-i+1) z[i] = z[k];
    else {
      L = i;
      while (R < n && s[R-L] == s[R]) R++;
      z[i] = R-L; R--;
    }
  }
}
return z;
}

mt19937 rnd(time(0));

struct subj
{
    ll adder;
    ll cur;
    ll l;
    ll r;
    ll b;
    ll idx;
};

bool sortsubj(subj x, subj y)
{
    if (x.adder==y.adder) return x.idx>y.idx;
    return x.adder>y.adder;
}

ll n, x;
ll total = 0;
vector<subj> kek;
vector<ll> pref_sum;

bool check( ll k)
{
    ll full = k/x;
    ll left = k%x;
    ll totalcur = total+pref_sum[full];
    for (int i = 0; i<n; i++)
    {
        ll newtotal = totalcur - kek[i].cur;
        if (i<full) {newtotal-=kek[i].adder; newtotal+=kek[full].adder;}
        if (left<kek[i].b) newtotal+=kek[i].l*(left-kek[i].b);
        else newtotal+=kek[i].r*(left-kek[i].b);
        if (newtotal>=0) return true;
    }
    return false;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr);


    cin>>n>>x;
    vector<ll> b(n);
    vector<ll> l(n);
    vector<ll> r(n);
    for (int i = 0; i<n; i++) cin>>b[i]>>l[i]>>r[i];
    vector<ll> low(n);
    vector<ll> adder(n);
    for (int i = 0; i<n; i++) 
    {
        low[i] = -l[i]*b[i];
        adder[i] = r[i]*(x-b[i])-low[i];
    }
    
    for (int i = 0; i<n; i++)
    {
        subj x;
        x.adder = adder[i];
        x.cur = low[i];
        x.l = l[i];
        x.r = r[i];
        x.b = b[i];
        x.idx = i;
        kek.push_back(x);
    }
    sort(kek.begin(), kek.end(), sortsubj);
    
    //for (auto it: kek) cout<<it.adder<<' '<<it.cur<<' '<<it.l<<' '<<it.r<<' '<<it.b<<' '<<it.idx<<endl;
    
    
    
    for (int i = 0; i<n; i++) total+=kek[i].cur;
    pref_sum.resize(n+1);
    pref_sum[0] = 0;
    for (int i = 1; i<=n; i++) pref_sum[i] = kek[i-1].adder + pref_sum[i-1];
    bool checker = false;
    for (int i = 0; i<n; i++) if (b[i]!=0) checker = true;
    if (!checker) {cout<<0; return 0;}
    ll L = 0;
    ll R = n*x;
    while (R>L+1)
    {
        ll mid = (L+R)/2;
        if (check(mid)) R = mid;
        else L = mid;
    }
    cout<<R;

}

Measured memory_bytes: 12400