#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<unordered_map>
#include<climits>
#include<cstdlib>
#include<cmath>
#include<string>
#include<iomanip>
#include<bitset>

using namespace std;

#define ll long long int

ll const MOD = 1000000007;
ll const INF = (long long int)1 << 61;

ll mypow(ll x,ll n){
    ll ret = 1;
    while(n > 0){
        if(n&1){
            ret = (ret*x)%MOD;
        }
        x = (x*x)%MOD;
        n >>= 1;
    }
    return ret;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
 
    ll n,x;
    cin >> n >> x;

    vector<ll> b(n),l(n),u(n);
    for(int i = 0; i < n; i++){
        cin >> b[i] >> l[i] >> u[i];
    }

    vector<pair<ll,ll>> s;
    for(int i = 0; i < n; i++){
        s.push_back(make_pair((x - b[i])*u[i] + b[i]*l[i],(ll)i));
    }

    sort(s.begin(),s.end(),greater<pair<ll,ll>>());

    vector<ll> lsum(n,0),usum(n,0);

    lsum[0] = b[s[0].second]*l[s[0].second];
    usum[0] = b[s[0].second]*u[s[0].second];

    for(int i = 1; i < n; i++){
        lsum[i] = lsum[i-1] + b[s[i].second]*l[s[i].second];
        usum[i] = usum[i-1] + b[s[i].second]*u[s[i].second];
    }

    vector<ll> us(n,0);
    us[0] = u[s[0].second];
    for(int i = 1; i < n; i++){
        us[i] = us[i-1] + u[s[i].second];
    }

    ll mynum = 0;
    ll ok = x*n;
    ll ng = -1;

    vector<ll> ux(n,0);
    ux[0] = u[s[0].second];

    for(int i = 1; i < n; i++){
        ux[i] = ux[i-1] + u[s[i].second];
    }

    auto eval = [&](ll m)->bool{
        bool ret = false;
        for(int i = 0; i < n; i++){
            ll q = m/x;
            ll r = m - q*x;
            ll ut = 0;
            ll lt = 0;
            if(q == 0){
                if(q < i){
                    lt = lsum[n-1] - lsum[i] + lsum[i-1];
                }else{
                    lt = lsum[n-1] - lsum[q];
                }
            }else{ 
                if(q < i){
                    ut = usum[q-1];
                    lt = lsum[n-1] - lsum[i] + lsum[i-1] - lsum[q-1];
                }else{
                    ut = (i > 0 ?usum[i-1] : 0) + usum[q] - usum[i];
                    lt = lsum[n-1] - lsum[q];
                }
            }
            if(r > 0){
                if(r < b[s[i].second]){
                    if(q == 0){
                        if(q < i){
                            if(ut + lt + l[s[i].second]*b[s[i].second] <= r*l[s[i].second]){
                                ret = true;
                            }
                        }else{
                            if(ut + lt + l[s[i].second]*b[s[i].second] <= r*l[s[i].second]){
                                ret = true;
                            }
                        }
                    }else{
                        if(q < i){
                            if(ut + lt + l[s[i].second]*b[s[i].second] <= ux[q-1]*x + r*l[s[i].second]){
                                ret = true;
                            }
                        }else{
                            if(ut + lt + l[s[i].second]*b[s[i].second] <= (i > 0 ? ux[i-1]*x : 0) + ux[q]*x - ux[i]*x + r*l[s[i].second]){
                                ret = true;
                            }
                        }
                    }
                }else{
                    if(q == 0){
                        if(q < i){
                            if(ut + lt + u[s[i].second]*b[s[i].second] <= r*u[s[i].second]){
                                ret = true;
                            }
                        }else{
                            if(ut + lt + u[s[i].second]*b[s[i].second] <= r*u[s[i].second]){
                                ret = true;
                            }
                        }
                    }else{
                        if(q < i){
                            if(ut + lt + u[s[i].second]*b[s[i].second] <= ux[q-1]*x + r*u[s[i].second]){
                                ret = true;
                            }
                        }else{
                            if(ut + lt + u[s[i].second]*b[s[i].second] <= (i > 0 ? ux[i-1]*x : 0) + ux[q]*x - ux[i]*x + r*u[s[i].second]){
                                ret = true;
                            }
                        }
                    }
                }
            }else{
                if(q != 0){
                    if(q < i){
                        if(ut + lt + l[s[i].second]*b[s[i].second] <= ux[q-1]*x){
                            ret = true;
                        }
                    }else{
                        if(ut + lt + l[s[i].second]*b[s[i].second] <= (i > 0 ? ux[i-1]*x : 0) + ux[q]*x - ux[i]*x){
                            ret = true;
                        }
                    }
                }else{
                    if(ut + lt + l[s[i].second]*b[s[i].second] <= 0){
                        ret = true;
                    }
                }
            }
        }
        return ret;
    };

    while(abs(ok - ng) > 1){
        ll middle = (ok + ng)/2;
        if(eval(middle)){
            ok = middle;
        }else{
            ng = middle;
        }
    }

    cout << ok << endl;

    return 0;
}

Measured memory_bytes: 7280