#include <bits/stdc++.h>
#define pb push_back
#define pll pair <ll, ll>
#define mp make_pair
#define pyshnapyshnakaa ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define x first
#define y second
#pragma GCC optimize("O3")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
#define plll pair <pair <ll, ll>, ll>
#define pllll pair <pair <ll, ll>, pair <ll, ll> >
#define psl pair <string, ll>
#define pld pair <ld, ld>
#define all(a) a.begin(), a.end()
#define vvl vector <vector <ll> >
 
typedef long long ll;
 
typedef long double ld;
 
using namespace std;
 
const ll maxn = 1e6 + 100;
const ll inf = 1e16;

ll n, m, k, t;

ll B[maxn];

ll L[maxn];

ll U[maxn];

ll V[maxn];

ll C[maxn];

// ll V2[maxn];

// vector <ld> P;
// vector <ll> IND;
vector <ll> I;

// ld intersect(ll i, ll j) {

// }

bool cmp(ll i, ll j) {
    return V[i] > V[j];
    // if (U[i] == U[j]) {
    //     return C[i] > C[j];
    // }
    // return U[i] < U[j];
}

ll need = 0;

ll get_val(ll i, ll x) {
    ll ans = max(0LL, (x - B[i])) * U[i] + L[i] * min(B[i], x);
    // cout << i << " " << x << " " << ans << endl;
    return ans;
}

bool can(ll x) {
    ll q, w, e;
    ll x1 = x;
    // cout << need << endl;
    ll need1 = need;
    ll i1 = min(x / m, n);
    ll sum1 = 0, sum2 = 0;
    for (q = 0; q < i1; q++) {
        ll i = I[q];
        sum1 += V[i];
    }
    if (i1 != n) {
        sum2 = sum1 + V[I[i1]];
    }
    else {
        sum2 = sum1;
    }
    if (sum1 >= need1) {
        return 1;
    }
    for (q = 0; q < n; q++) {
        ll i = I[q];
        if (q < i1) {
            ll nval = get_val(i, x % m) + sum2 - V[i];
            if (nval >= need1) {
                return 1;
            }
        }
        else {
            ll nval = get_val(i, x % m) + sum1;
            if (nval >= need1) {
                return 1;
            }
        }
    }
    // for (q = 0; q < n; q++) {
    //     if (x1 <= m) {
    //         for (w = q; w < n; w++) {
    //             ll i = I[w];
    //             ll val = get_val(i, x1);
    //             if (val >= need1) {
    //                 return 1;
    //             }
    //         }
    //         break;
    //     }
    //     ll i = I[q];
    //     need1 -= V[i];
    //     // cout <<need1 << " NEED" << endl;
    //     x1 -= m;
    //     if (need1 < 0) {
    //         return 1;
    //     }
    //     if (x1 <= m) {
    //         for (w = q + 1; w < n; w++) {
    //             ll i = I[w];
    //             ll val = get_val(i, x1);
    //             if (val >= need1) {
    //                 return 1;
    //             }
    //         }
    //         break;
    //     }
    // }
    return 0;
}

int main() {
    pyshnapyshnakaa;
    ll q, w, e, a, b, c, d;
    string s;
    cin >> n >> m;
    for (q = 0; q < n; q++) {
        cin >> B[q] >> L[q] >> U[q];
        need += B[q] * L[q];
        V[q] = U[q] * m - B[q] * (U[q] - L[q]); 
        C[q] = B[q] * (L[q] - U[q]);
    }
    for (q = 0; q < n; q++) {
        I.pb(q);
    }
    sort(all(I), cmp);
    ll L1 = -1, R1 = inf, M1;
    // cout << V[0] << endl;
    // cout << can(115) << endl;
    // return 0;
    while (R1 - L1 > 1) {
        // cout << L1 << " " << R1 << endl;
        M1 = (R1 + L1) / 2;
        if (can(M1)) {
            // cout << "CAN " << M1 << endl;
            R1 = M1;
        }
        else {
            L1 = M1;
        }
    }
    cout << R1;
    return 0;
}

Measured memory_bytes: 8212