#include<bits/stdc++.h>

using namespace std;

using ui = uint32_t;

uint64_t compute_happiness(
    const uint64_t S, const uint64_t total_a_pizzas, const uint64_t total_b_pizzas,
    const vector<ui> &as, const vector<ui> &bs, const vector<ui> &ss, const vector<ui> &is
    ) {

    // cerr << "Will try with " << total_a_pizzas << " " << total_b_pizzas << endl;

    uint64_t H = 0;
    uint64_t s = 0;
    uint64_t i = 0;

    uint64_t togo = 0;

    const auto total_a_slices = S * total_a_pizzas;

    for (; i != is.size() && s != total_a_slices; ++i) {
        const auto person = is[i];
        const auto slices = ss[person];

        const auto to_eat = s + slices <= total_a_slices ? slices : total_a_slices - s;

        s += to_eat;
        H += to_eat * as[person];

        togo = slices - to_eat;

        // cerr << "Person " << i << " ate " << to_eat << " (" << togo << " togo)" << ", bumping to (slices, happiness): " << "(" << s << ", " << H << ")" << endl;
    }

    if (i != 0) {
        H += togo * bs[is[i - 1]];

        s = togo;

        // cerr << "Account for togo, bumping to (slices, happiness): " << "(" << s << ", " << H << ")" << endl;
    }

    for (; i != is.size(); ++i) {
        const auto person = is[i];
        const auto slices = ss[person];

        H += slices * bs[person];
        // cerr << "Person " << i << " ate " << slices << ", bumping to (slices, happiness): " << "(" << s << ", " << H << ")" << endl;

    }

    // cerr << "=========== final happiess: " << H << endl;

    return H;
}

int main() {

    ui N, S;
    uint64_t H, ats, bts, uts;
    cin >> N >> S;

    H = 0;
    ats = 0;  // total slices of a (first pizza)
    bts = 0;  // total slices of b (second pizza)
    uts = 0;  // total slices of undecided

    auto ss = vector<ui>(N, 0);
    auto as = vector<ui>(N, 0);
    auto bs = vector<ui>(N, 0);
    auto is = vector<ui>(N, 0);

    for (ui i = 0; i != N; ++i) {
        cin >> ss[i];
        cin >> as[i];
        cin >> bs[i];

        is[i] = i;

        if (as[i] == bs[i]) {
            uts += ss[i];
        } else if (as[i] > bs[i]) {
            ats += ss[i];
        } else {
            bts += ss[i];
        }
    }

    sort(
        is.begin()
        , is.end()
        , [&as, &bs] (const auto &lhs, const auto &rhs) {
              // Reorder the elements so that:
              // 1. In the fron you have people who will be very upset if they don't eat pizza of type a
              // 2. At the back you have people who will be very upset if they don't eat pizza of type b
              // 3. In the middle you have people who don't have such a strong difference in happiness for pizza's a or b
              // Somewhere in category 3 people will have to eat "the wrong" single pizza
              if (as[lhs] >= bs[lhs] && as[rhs] >= bs[rhs]) {
                  // Both prefer pizza A, figure out who will be more upset and push to front
                  if (as[lhs] - bs[lhs] >= as[rhs] - bs[rhs]) {
                      return true;
                  } else {
                      return false;
                  }
              } else if (as[lhs] < bs[lhs] && as[rhs] < bs[rhs]) {
                  // Both prefer pizza B, figure out who will be more upset and push to back
                  if (bs[lhs] - as[lhs] <= bs[rhs] - as[rhs]) {
                      return true;
                  } else {
                      return false;
                  }
              } else if (as[lhs] >= bs[lhs] && as[rhs] < bs[rhs]) {
                  // Left prefers A, while right prefers B, push left to front and right to back
                  return true;
              } else if (as[lhs] < bs[lhs] && as[rhs] >= bs[rhs]) {
                  // Right prefers Bm while left prefers A, push left to back and right to front
                  return false;
              }

              // This should never be reached, return false to pelase the compiler
              // throw runtime_error("Comparator is broken");
              return false;
          }
        );

    // cerr << "The order is:" << endl;
    // for (auto i : is) {
    //     cerr << ss[i] << " " << as[i] << " " << bs[i] << endl;
    // } cerr << "==============" << endl;

    uint64_t total_pizzas = (ats + bts + uts + S - 1) / S;
    uint64_t total_a_pizzas = (ats + S - 1) / S;
    uint64_t total_b_pizzas = (bts + S - 1) / S;

    if (total_a_pizzas + total_b_pizzas <= total_pizzas) {
        // everybody eats what they want, optimal happiness
        for (ui i = 0; i != N; ++i) {
            if (as[i] >= bs[i]) {
                H += ((uint64_t) ss[i]) * as[i];
            } else {
                H += ((uint64_t) ss[i]) * bs[i];
            }
        }
    } else {
        // there is one pizza that must be split, suboptimal happiness
        // total_a_pizzas + total_b_pizzas == total_pizzas + 1
        // just bruteforce which pizza type should suffer: first or second?

        auto Ha = compute_happiness(S, total_a_pizzas, total_b_pizzas - 1, as, bs, ss, is);
        auto Hb = compute_happiness(S, total_a_pizzas - 1, total_b_pizzas, as, bs, ss, is);

        if (Ha >= Hb) {
            H = Ha;
        } else {
            H = Hb;
        }
    }

    cout << H << endl;
}


#include<bits/stdc++.h>

using namespace std;

int main() {
    int A; cin >> A;

    ios_base::sync_with_stdio(false);

    cout << 1E6 << " " << A << endl;
    cout << 1 << " ";

    for (int i = 0; i != A - 1; ++i) {
        cout << 1E6 - i << " ";
    } cout << endl;
}


#include<bits/stdc++.h>

using namespace std;

using ui = uint32_t;

ui compute_happiness(
    const uint64_t S, const uint64_t total_a_pizzas, const uint64_t total_b_pizzas,
    const vector<ui> &as, const vector<ui> &bs, const vector<ui> &ss, const vector<ui> &is
    ) {

    uint64_t H = 0;
    uint64_t s = 0;
    uint64_t i = 0;

    uint64_t togo = 0;

    const auto total_a_slices = S * total_a_pizzas;

    for (; i != is.size() && s != total_a_slices; ++i) {
        const auto person = is[i];
        const auto slices = ss[person];

        const auto to_eat = s + slices <= total_a_slices ? slices : total_a_slices - s;

        s += to_eat;
        H += to_eat * as[person];

        togo = slices - to_eat;

        cerr << "Person " << i << " ate " << to_eat << " (" << togo << " togo)" << ", bumping to (slices, happiness): " << "(" << s << ", " << H << ")" << endl;
    }

    if (i != is.size()) {
        H += togo * bs[is[i - 1]];

        s = togo;

        cerr << "Account for togo, bumping to (slices, happiness): " << "(" << s << ", " << H << ")" << endl;
    }

    for (; i != is.size(); ++i) {
        const auto person = is[i];
        const auto slices = ss[person];

        H += slices * bs[person];
        cerr << "Person " << i << " ate " << slices << ", bumping to (slices, happiness): " << "(" << s << ", " << H << ")" << endl;

    }

    cerr << "=========== final happiess: " << H << endl;

    return H;
}

int main() {

    ui N, S;
    uint64_t H, ats, bts, uts;
    cin >> N >> S;

    H = 0;
    ats = 0;  // total slices of a (first pizza)
    bts = 0;  // total slices of b (second pizza)
    uts = 0;  // total slices of undecided

    auto ss = vector<ui>(N, 0);
    auto as = vector<ui>(N, 0);
    auto bs = vector<ui>(N, 0);
    auto is = vector<ui>(N, 0);

    for (ui i = 0; i != N; ++i) {
        cin >> ss[i];
        cin >> as[i];
        cin >> bs[i];

        is[i] = i;

        if (as[i] == bs[i]) {
            uts += ss[i];
        } else if (as[i] > bs[i]) {
            ats += ss[i];
        } else {
            bts += ss[i];
        }
    }

    sort(
        is.begin()
        , is.end()
        , [&as, &bs] (const auto &lhs, const auto &rhs) {
              // Reorder the elements so that:
              // 1. In the fron you have people who will be very upset if they don't eat pizza of type a
              // 2. At the back you have people who will be very upset if they don't eat pizza of type b
              // 3. In the middle you have people who don't have such a strong difference in happiness for pizza's a or b
              // Somewhere in category 3 people will have to eat "the wrong" single pizza
              if (as[lhs] >= bs[lhs] && as[rhs] >= bs[rhs]) {
                  // Both prefer pizza A, figure out who will be more upset and push to front
                  if (as[lhs] - bs[lhs] >= as[rhs] - bs[rhs]) {
                      return true;
                  } else {
                      return false;
                  }
              } else if (as[lhs] < bs[lhs] && as[rhs] < bs[rhs]) {
                  // Both prefer pizza B, figure out who will be more upset and push to back
                  if (bs[lhs] - as[lhs] <= bs[rhs] - as[rhs]) {
                      return true;
                  } else {
                      return false;
                  }
              } else if (as[lhs] >= bs[lhs] && as[rhs] < bs[rhs]) {
                  // Left prefers A, while right prefers B, push left to front and right to back
                  return true;
              } else if (as[lhs] < bs[lhs] && as[rhs] >= bs[rhs]) {
                  // Right prefers Bm while left prefers A, push left to back and right to front
                  return false;
              }

              // This should never be reached, return false to pelase the compiler
              // throw runtime_error("Comparator is broken");
              return false;
          }
        );

    // cerr << "The order is:" << endl;
    // for (auto i : is) {
    //     cerr << ss[i] << " " << as[i] << " " << bs[i] << endl;
    // } cerr << "==============" << endl;

    uint64_t total_pizzas = (ats + bts + uts + S - 1) / S;
    uint64_t total_a_pizzas = (ats + S - 1) / S;
    uint64_t total_b_pizzas = (bts + S - 1) / S;

    if (total_a_pizzas + total_b_pizzas <= total_pizzas) {
        // everybody eats what they want, optimal happiness
        for (ui i = 0; i != N; ++i) {
            if (as[i] >= bs[i]) {
                H += ((uint64_t) ss[i]) * as[i];
            } else {
                H += ((uint64_t) ss[i]) * bs[i];
            }
        }
    } else {
        // there is one pizza that must be split, suboptimal happiness
        // total_a_pizzas + total_b_pizzas == total_pizzas + 1
        // just bruteforce which pizza type should suffer: first or second?

        auto Ha = compute_happiness(S, total_a_pizzas, total_b_pizzas - 1, as, bs, ss, is);
        auto Hb = compute_happiness(S, total_a_pizzas - 1, total_b_pizzas, as, bs, ss, is);

        if (Ha >= Hb) {
            H = Ha;
        } else {
            H = Hb;
        }
    }

    cout << H << endl;
}


#include<bits/stdc++.h>

using namespace std;

using ui = uint32_t;

uint64_t compute_happiness(
    const uint64_t S, const uint64_t total_a_pizzas, const uint64_t total_b_pizzas,
    const vector<ui> &as, const vector<ui> &bs, const vector<ui> &ss, const vector<ui> &is
    ) {

    // cerr << "Will try with " << total_a_pizzas << " " << total_b_pizzas << endl;

    uint64_t H = 0;
    uint64_t s = 0;
    uint64_t i = 0;

    uint64_t togo = 0;

    const auto total_a_slices = S * total_a_pizzas;

    for (; i != is.size() && s != total_a_slices; ++i) {
        const auto person = is[i];
        const auto slices = ss[person];

        const auto to_eat = s + slices <= total_a_slices ? slices : total_a_slices - s;

        s += to_eat;
        H += to_eat * as[person];

        togo = slices - to_eat;

        // cerr << "Person " << i << " ate " << to_eat << " (" << togo << " togo)" << ", bumping to (slices, happiness): " << "(" << s << ", " << H << ")" << endl;
    }

    if (i != is.size()) {
        H += togo * bs[is[i - 1]];

        s = togo;

        // cerr << "Account for togo, bumping to (slices, happiness): " << "(" << s << ", " << H << ")" << endl;
    }

    for (; i != is.size(); ++i) {
        const auto person = is[i];
        const auto slices = ss[person];

        H += slices * bs[person];
        // cerr << "Person " << i << " ate " << slices << ", bumping to (slices, happiness): " << "(" << s << ", " << H << ")" << endl;

    }

    // cerr << "=========== final happiess: " << H << endl;

    return H;
}

int main() {

    ui N, S;
    uint64_t H, ats, bts, uts;
    cin >> N >> S;

    H = 0;
    ats = 0;  // total slices of a (first pizza)
    bts = 0;  // total slices of b (second pizza)
    uts = 0;  // total slices of undecided

    auto ss = vector<ui>(N, 0);
    auto as = vector<ui>(N, 0);
    auto bs = vector<ui>(N, 0);
    auto is = vector<ui>(N, 0);

    for (ui i = 0; i != N; ++i) {
        cin >> ss[i];
        cin >> as[i];
        cin >> bs[i];

        is[i] = i;

        if (as[i] == bs[i]) {
            uts += ss[i];
        } else if (as[i] > bs[i]) {
            ats += ss[i];
        } else {
            bts += ss[i];
        }
    }

    sort(
        is.begin()
        , is.end()
        , [&as, &bs] (const auto &lhs, const auto &rhs) {
              // Reorder the elements so that:
              // 1. In the fron you have people who will be very upset if they don't eat pizza of type a
              // 2. At the back you have people who will be very upset if they don't eat pizza of type b
              // 3. In the middle you have people who don't have such a strong difference in happiness for pizza's a or b
              // Somewhere in category 3 people will have to eat "the wrong" single pizza
              if (as[lhs] >= bs[lhs] && as[rhs] >= bs[rhs]) {
                  // Both prefer pizza A, figure out who will be more upset and push to front
                  if (as[lhs] - bs[lhs] >= as[rhs] - bs[rhs]) {
                      return true;
                  } else {
                      return false;
                  }
              } else if (as[lhs] < bs[lhs] && as[rhs] < bs[rhs]) {
                  // Both prefer pizza B, figure out who will be more upset and push to back
                  if (bs[lhs] - as[lhs] <= bs[rhs] - as[rhs]) {
                      return true;
                  } else {
                      return false;
                  }
              } else if (as[lhs] >= bs[lhs] && as[rhs] < bs[rhs]) {
                  // Left prefers A, while right prefers B, push left to front and right to back
                  return true;
              } else if (as[lhs] < bs[lhs] && as[rhs] >= bs[rhs]) {
                  // Right prefers Bm while left prefers A, push left to back and right to front
                  return false;
              }

              // This should never be reached, return false to pelase the compiler
              // throw runtime_error("Comparator is broken");
              return false;
          }
        );

    // cerr << "The order is:" << endl;
    // for (auto i : is) {
    //     cerr << ss[i] << " " << as[i] << " " << bs[i] << endl;
    // } cerr << "==============" << endl;

    uint64_t total_pizzas = (ats + bts + uts + S - 1) / S;
    uint64_t total_a_pizzas = (ats + S - 1) / S;
    uint64_t total_b_pizzas = (bts + S - 1) / S;

    if (total_a_pizzas + total_b_pizzas <= total_pizzas) {
        // everybody eats what they want, optimal happiness
        for (ui i = 0; i != N; ++i) {
            if (as[i] >= bs[i]) {
                H += ((uint64_t) ss[i]) * as[i];
            } else {
                H += ((uint64_t) ss[i]) * bs[i];
            }
        }
    } else {
        // there is one pizza that must be split, suboptimal happiness
        // total_a_pizzas + total_b_pizzas == total_pizzas + 1
        // just bruteforce which pizza type should suffer: first or second?

        auto Ha = compute_happiness(S, total_a_pizzas, total_b_pizzas - 1, as, bs, ss, is);
        auto Hb = compute_happiness(S, total_a_pizzas - 1, total_b_pizzas, as, bs, ss, is);

        if (Ha >= Hb) {
            H = Ha;
        } else {
            H = Hb;
        }
    }

    cout << H << endl;
}


#include<bits/stdc++.h>

using namespace std;

using ui = uint32_t;

uint64_t compute_happiness(
    const uint64_t S, const uint64_t total_a_pizzas, const uint64_t total_b_pizzas,
    const vector<ui> &as, const vector<ui> &bs, const vector<ui> &ss, const vector<ui> &is
    ) {

    // cerr << "Will try with " << total_a_pizzas << " " << total_b_pizzas << endl;

    uint64_t H = 0;
    uint64_t s = 0;
    uint64_t i = 0;

    uint64_t togo = 0;

    const auto total_a_slices = S * total_a_pizzas;

    for (; i < is.size() && s < total_a_slices; ++i) {
        const auto person = is[i];
        const auto slices = ss[person];

        const auto to_eat = s + slices <= total_a_slices ? slices : total_a_slices - s;

        s += to_eat;
        H += to_eat * as[person];

        togo = slices - to_eat;

        // cerr << "Person " << i << " ate " << to_eat << " (" << togo << " togo)" << ", bumping to (slices, happiness): " << "(" << s << ", " << H << ")" << endl;
    }

    if (i > 0 && i < is.size()) {
        H += togo * bs[is[i - 1]];

        s = togo;

        // cerr << "Account for togo, bumping to (slices, happiness): " << "(" << s << ", " << H << ")" << endl;
    }

    for (; i < is.size(); ++i) {
        const auto person = is[i];
        const auto slices = ss[person];

        H += slices * bs[person];
        // cerr << "Person " << i << " ate " << slices << ", bumping to (slices, happiness): " << "(" << s << ", " << H << ")" << endl;

    }

    // cerr << "=========== final happiess: " << H << endl;

    return H;
}

int main() {

    ui N, S;
    uint64_t H, ats, bts, uts;
    cin >> N >> S;

    H = 0;
    ats = 0;  // total slices of a (first pizza)
    bts = 0;  // total slices of b (second pizza)
    uts = 0;  // total slices of undecided

    auto ss = vector<ui>(N, 0);
    auto as = vector<ui>(N, 0);
    auto bs = vector<ui>(N, 0);
    auto is = vector<ui>(N, 0);

    for (ui i = 0; i != N; ++i) {
        cin >> ss[i];
        cin >> as[i];
        cin >> bs[i];

        is[i] = i;

        if (as[i] == bs[i]) {
            uts += ss[i];
        } else if (as[i] > bs[i]) {
            ats += ss[i];
        } else {
            bts += ss[i];
        }
    }

    sort(
        is.begin()
        , is.end()
        , [&as, &bs] (const auto &lhs, const auto &rhs) {
              // Reorder the elements so that:
              // 1. In the fron you have people who will be very upset if they don't eat pizza of type a
              // 2. At the back you have people who will be very upset if they don't eat pizza of type b
              // 3. In the middle you have people who don't have such a strong difference in happiness for pizza's a or b
              // Somewhere in category 3 people will have to eat "the wrong" single pizza
              if (as[lhs] >= bs[lhs] && as[rhs] >= bs[rhs]) {
                  // Both prefer pizza A, figure out who will be more upset and push to front
                  if (as[lhs] - bs[lhs] >= as[rhs] - bs[rhs]) {
                      return true;
                  } else {
                      return false;
                  }
              } else if (as[lhs] < bs[lhs] && as[rhs] < bs[rhs]) {
                  // Both prefer pizza B, figure out who will be more upset and push to back
                  if (bs[lhs] - as[lhs] <= bs[rhs] - as[rhs]) {
                      return true;
                  } else {
                      return false;
                  }
              } else if (as[lhs] >= bs[lhs] && as[rhs] < bs[rhs]) {
                  // Left prefers A, while right prefers B, push left to front and right to back
                  return true;
              } else if (as[lhs] < bs[lhs] && as[rhs] >= bs[rhs]) {
                  // Right prefers Bm while left prefers A, push left to back and right to front
                  return false;
              }

              // This should never be reached, return false to pelase the compiler
              // throw runtime_error("Comparator is broken");
              return false;
          }
        );

    // cerr << "The order is:" << endl;
    // for (auto i : is) {
    //     cerr << ss[i] << " " << as[i] << " " << bs[i] << endl;
    // } cerr << "==============" << endl;

    uint64_t total_pizzas = (ats + bts + uts + S - 1) / S;
    uint64_t total_a_pizzas = (ats + S - 1) / S;
    uint64_t total_b_pizzas = (bts + S - 1) / S;

    if (total_a_pizzas + total_b_pizzas <= total_pizzas) {
        // everybody eats what they want, optimal happiness
        for (ui i = 0; i != N; ++i) {
            if (as[i] >= bs[i]) {
                H += ((uint64_t) ss[i]) * as[i];
            } else {
                H += ((uint64_t) ss[i]) * bs[i];
            }
        }
    } else {
        // there is one pizza that must be split, suboptimal happiness
        // total_a_pizzas + total_b_pizzas == total_pizzas + 1
        // just bruteforce which pizza type should suffer: first or second?

        auto Ha = compute_happiness(S, total_a_pizzas, total_b_pizzas - 1, as, bs, ss, is);
        auto Hb = compute_happiness(S, total_a_pizzas - 1, total_b_pizzas, as, bs, ss, is);

        if (Ha >= Hb) {
            H = Ha;
        } else {
            H = Hb;
        }
    }

    cout << H << endl;
}


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

//tipos
#define int long long
#define ll long long
#define ull unsigned long long
#define ii pair< int , int>
#define vi vector<int>
#define vii vector< pair<int,int> >

//comandos
#define pb push_back
#define mp make_pair
#define sz(x) ((int)x.size())
#define ff first
#define ss second

//constantes
#define MOD 1000000007
#define MAX 31625
#define oo 100000000000005LL

int n, s, k, total;
int N[100005], A[100005], B[100005];

struct doido{
    int x, a, b;

    bool operator < (doido j) const{
        return (a-b) > (j.a-j.b);
    }
};

vector<doido> ordem;

int F(int a){
    int pa = a*s;
    int pb = (k-a)*s;
    int ans =0;

    for(int i = 0; i < ordem.size(); i++){

        int ca = min(pa, ordem[i].x);
        ans += ordem[i].a*ca;
        pa -= ca;

        if(ca < ordem[i].x){
            int cb = min(pb, ordem[i].x - ca);
            ans += ordem[i].b*cb;
            pb -= cb;
        }

    }

    pa = a*s;
    pb = (k-a)*s;
    int ans2 =0;

    for(int i = ordem.size()-1; i>= 0; i--){

        int cb = min(pb, ordem[i].x);
        ans2 += ordem[i].b*cb;
        pb -= cb;

        if(cb < ordem[i].x){
            int ca = min(pa, ordem[i].x - cb);
            ans2 += ordem[i].a*ca;
            pa -= ca;
        }
    }

    return max(ans, ans2);

}

main(){

    scanf("%lld %lld", &n, &s);
    for(int i = 0; i < n; i++){
        scanf("%lld %lld %lld", &N[i], &A[i], &B[i]);
        total += N[i];
        ordem.push_back( {N[i], A[i], B[i]} );

    }
    sort(ordem.begin(), ordem.end());

    k = (total/s) + (total%s?1:0);

    int e = 0, d = k;

    while(e < d){
        int m2 = (e+d)/2;
        int m1 = (e+m2)/2;
        int m3 = (m2+d)/2;
        if( m3 < d ) m3++;

        int f1 = F(m1);
        int f2 = F(m2);
        int f3 = F(m3);

        if(f2 >= f1 and f2 >= f3){
            e = min(m1+1, m2);
            d = max(m3-1, m2);
        }else if ( f1 >= f2 and f1 >= f3 ){
            d = m2-1;
        }else {
            e = m2+1;

        }

        if(f3 >= f1 and f3 >= f2){
            e = m2+1;
        }else if ( f2 >= f1 and f2 >= f3 ){
            e = min(m1+1, m2);
            d = max(m3-1, m2);
        }else {
            d = m2-1;
        }
    }

    printf("%lld\n", F(e));

}


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

//tipos
#define int long long
#define ll long long
#define ull unsigned long long
#define ii pair< int , int>
#define vi vector<int>
#define vii vector< pair<int,int> >

//comandos
#define pb push_back
#define mp make_pair
#define sz(x) ((int)x.size())
#define ff first
#define ss second

//constantes
#define MOD 1000000007
#define MAX 31625
#define oo 100000000000005LL

int n, s, k, total;
int N[100005], A[100005], B[100005];

struct doido{
    int x, a, b;

    bool operator < (doido j) const{
        return (a-b) > (j.a-j.b);
    }
};

vector<doido> ordem;
int ta, tb, pa, pb;

main(){

    scanf("%lld %lld", &n, &s);
    for(int i = 0; i < n; i++){
        scanf("%lld %lld %lld", &N[i], &A[i], &B[i]);
        total += N[i];
        ordem.push_back( {N[i], A[i], B[i]} );
        if(A[i] > B[i]) ta+=N[i];
        else if(B[i] >= A[i] )tb += N[i];
    }

    sort(ordem.begin(), ordem.end());
    pa = ta - ta%s;
    pb = tb - tb%s;
    k = (total/s) + (total%s?1:0);

    int ans = 0;
    for(int i = 0; i < ordem.size(); i++){
        int q = min(ordem[i].x, pa);
        pa -= q;
        ans += q*ordem[i].a;
        ordem[i].x -= q;
    }
    reverse(ordem.begin(), ordem.end());
    for(int i = 0; i < ordem.size(); i++){
        int q = min(ordem[i].x, pb);
        pb -= q;
        ans += q*ordem[i].b;
        ordem[i].x -= q;
    }

    int opa = 0, opb = 0;
    for(int i = 0; i < ordem.size(); i++){
        opa += ordem[i].x*ordem[i].a;
        opb += ordem[i].x*ordem[i].b;
    }
    cout << ans + max(opa, opb) << endl;
}


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

//tipos
#define int long long
#define ll long long
#define ull unsigned long long
#define ii pair< int , int>
#define vi vector<int>
#define vii vector< pair<int,int> >

//comandos
#define pb push_back
#define mp make_pair
#define sz(x) ((int)x.size())
#define ff first
#define ss second

//constantes
#define MOD 1000000007
#define MAX 31625
#define oo 100000000000005LL

int n, s, k, total;
int N[100005], A[100005], B[100005];

struct doido{
    int x, a, b;

    bool operator < (doido j) const{
        return (a-b) > (j.a-j.b);
    }
};

vector<doido> ordem;
int ta, tb, pa, pb, te, pe;

main(){

    scanf("%lld %lld", &n, &s);
    for(int i = 0; i < n; i++){
        scanf("%lld %lld %lld", &N[i], &A[i], &B[i]);
        total += N[i];
        ordem.push_back( {N[i], A[i], B[i]} );
        if(A[i] > B[i]) ta+=N[i];
        else if(B[i] > A[i] )tb += N[i];
        else te += N[i];
    }

    if(te){
        if(ta%s > tb%s){
            int q = min(s-ta%s, te);
            ta += q;
            te -= q;
        }else{
            int q = min(s-tb%s, te);
            tb += q;
            te -= q;
        }
    }

    if(te){
        if(ta%s > tb%s){
            int q = min(s-ta%s, te);
            ta += q;
            te -= q;
        }else{
            int q = min(s-tb%s, te);
            tb += q;
            te -= q;
        }
    }

    sort(ordem.begin(), ordem.end());
    pa = ta - ta%s;
    pb = tb - tb%s;
    k = (total/s) + (total%s?1:0);

    int ans = 0;
    for(int i = 0; i < ordem.size(); i++){
        int q = min(ordem[i].x, pa);
        pa -= q;
        ans += q*ordem[i].a;
        ordem[i].x -= q;
    }
    reverse(ordem.begin(), ordem.end());
    for(int i = 0; i < ordem.size(); i++){
        int q = min(ordem[i].x, pb);
        pb -= q;
        ans += q*ordem[i].b;
        ordem[i].x -= q;
    }

    int opa = 0, opb = 0;
    for(int i = 0; i < ordem.size(); i++){
        opa += ordem[i].x*ordem[i].a;
        opb += ordem[i].x*ordem[i].b;
    }
    cout << ans + max(opa, opb) << endl;
}


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

//tipos
#define int long long
#define ll long long
#define ull unsigned long long
#define ii pair< int , int>
#define vi vector<int>
#define vii vector< pair<int,int> >

//comandos
#define pb push_back
#define mp make_pair
#define sz(x) ((int)x.size())
#define ff first
#define ss second

//constantes
#define MOD 1000000007
#define MAX 31625
#define oo 100000000000005LL

int n, s, k, total;
int N[100005], A[100005], B[100005];

struct doido{
    int x, a, b;

    bool operator < (doido j) const{
        return (a-b) > (j.a-j.b);
    }
};

vector<doido> ordem;
int ta, tb, pa, pb, te, pe;

main(){

    scanf("%lld %lld", &n, &s);
    for(int i = 0; i < n; i++){
        scanf("%lld %lld %lld", &N[i], &A[i], &B[i]);
        total += N[i];
        ordem.push_back( {N[i], A[i], B[i]} );
        if(A[i] > B[i]) ta+=N[i];
        else tb += N[i];

    }

    sort(ordem.begin(), ordem.end());
    pa = ta - ta%s;
    pb = tb - tb%s;

    int ans = 0;
    for(int i = 0; i < ordem.size(); i++){
        int q = min(ordem[i].x, pa);
        pa -= q;
        ans += q*ordem[i].a;
        ordem[i].x -= q;
    }
    reverse(ordem.begin(), ordem.end());
    for(int i = 0; i < ordem.size(); i++){
        int q = min(ordem[i].x, pb);
        pb -= q;
        ans += q*ordem[i].b;
        ordem[i].x -= q;
    }

    int opa = 0, opb = 0, ab = 0, ba = 0;
    for(int i = 0; i < ordem.size(); i++){
        opa += ordem[i].x*ordem[i].a;
        opb += ordem[i].x*ordem[i].b;

        int q = min(s, ordem[i].x);
        ordem[i].x-=q;
        ab += q*ordem[i].a;
        ba += q*ordem[i].b;

        ba += ordem[i].x*ordem[i].a;
        ab += ordem[i].x*ordem[i].b;

    }
    cout << ans + max(opa, max(opb, max(ba,ab) )) << endl;
}


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

//tipos
#define int long long
#define ll long long
#define ull unsigned long long
#define ii pair< int , int>
#define vi vector<int>
#define vii vector< pair<int,int> >

//comandos
#define pb push_back
#define mp make_pair
#define sz(x) ((int)x.size())
#define ff first
#define ss second

//constantes
#define MOD 1000000007
#define MAX 31625
#define oo 100000000000005LL

int n, s, k, total;
int N[100005], A[100005], B[100005];

struct doido{
    int x, a, b;

    bool operator < (doido j) const{
        return (a-b) > (j.a-j.b);
    }
};

vector<doido> ordem;

int F(int a){
    int pa = a*s;
    int pb = (k-a)*s;
    int ans =0;

    for(int i = 0; i < ordem.size(); i++){

        int ca = min(pa, ordem[i].x);
        ans += ordem[i].a*ca;
        pa -= ca;

        if(ca < ordem[i].x){
            int cb = min(pb, ordem[i].x - ca);
            ans += ordem[i].b*cb;
            pb -= cb;
        }
    }
    return ans;

}

main(){

    scanf("%lld %lld", &n, &s);
    for(int i = 0; i < n; i++){
        scanf("%lld %lld %lld", &N[i], &A[i], &B[i]);
        total += N[i];
        ordem.push_back( {N[i], A[i], B[i]} );

    }
    sort(ordem.begin(), ordem.end());

    k = (total/s) + (total%s?1:0);

    int e = 0, d = k;

    while(e < d){
        int m2 = (e+d)/2;
        int m1 = (e+m2)/2;
        int m3 = (m2+d)/2;
        int f1 = F(m1);
        int f2 = F(m2);
        int f3 = F(m3);
        /*
        printf("-----\n");
        printf("%lld - %lld\n", m1, f1);
        printf("%lld - %lld\n", m2, f2);
        printf("%lld - %lld\n", m3, f3);
        printf("----\n");
        */
        if(f2 >= f1 and f2 >= f3){
            e = m1+1;
            d = m3-1;
        }else if ( f1 >= f2 and f1 >= f3 ){
            d = m2-1;
        }else {
            e = m2+1;
        }
    }

    int m = (e+d)/2;
    printf("%lld\n",
           max( F(max(0LL, m)), max( F(min(k, m+1)), F(max(0LL, m+1)))));

/*
    printf("%lld\n", k);
    for(int i = 0; i < 5; i++){
        printf("%lld >> %lld\n", i, F(i));
    }
*/
}


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

//tipos
#define int long long
#define ll long long
#define ull unsigned long long
#define ii pair< int , int>
#define vi vector<int>
#define vii vector< pair<int,int> >

//comandos
#define pb push_back
#define mp make_pair
#define sz(x) ((int)x.size())
#define ff first
#define ss second

//constantes
#define MOD 1000000007
#define MAX 31625
#define oo 100000000000005LL

int n, s, k, total;
int N[100005], A[100005], B[100005];

struct doido{
    int x, a, b;

    bool operator < (doido j) const{
        return (a-b) > (j.a-j.b);
    }
};

vector<doido> ordem;
int ta, tb, pa, pb, te, pe;

main(){

    scanf("%lld %lld", &n, &s);
    for(int i = 0; i < n; i++){
        scanf("%lld %lld %lld", &N[i], &A[i], &B[i]);
        total += N[i];
        ordem.push_back( {N[i], A[i], B[i]} );
        if(A[i] > B[i]) ta+=N[i];
        else tb += N[i];

    }

    sort(ordem.begin(), ordem.end());
    pa = ta - ta%s;
    pb = tb - tb%s;

    int ans = 0;
    for(int i = 0; i < ordem.size(); i++){
        int q = min(ordem[i].x, pa);
        pa -= q;
        ans += q*ordem[i].a;
        ordem[i].x -= q;
    }
    reverse(ordem.begin(), ordem.end());
    for(int i = 0; i < ordem.size(); i++){
        int q = min(ordem[i].x, pb);
        pb -= q;
        ans += q*ordem[i].b;
        ordem[i].x -= q;
    }

    int opa = 0, opb = 0, ab = 0;
    for(int i = 0; i < ordem.size(); i++){
        opa += ordem[i].x*ordem[i].a;
        opb += ordem[i].x*ordem[i].b;

        if( (ta%s + tb%s)>s )
            ab += ordem[i].x*max(ordem[i].a, ordem[i].b);

    }
    cout << ans + max(opa, max(opb, ab)) << endl;
}


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

//tipos
#define int long long
#define ll long long
#define ull unsigned long long
#define ii pair< int , int>
#define vi vector<int>
#define vii vector< pair<int,int> >

//comandos
#define pb push_back
#define mp make_pair
#define sz(x) ((int)x.size())
#define ff first
#define ss second

//constantes
#define MOD 1000000007
#define MAX 31625
#define oo 100000000000005LL

int n, s, k, total;
int N[100005], A[100005], B[100005];

struct doido{
    int x, a, b;

    bool operator < (doido j) const{
        return (a-b) > (j.a-j.b);
    }
};

vector<doido> ordem;
int ta, tb, pa, pb, te, pe;

main(){

    scanf("%lld %lld", &n, &s);
    for(int i = 0; i < n; i++){
        scanf("%lld %lld %lld", &N[i], &A[i], &B[i]);
        total += N[i];
        ordem.push_back( {N[i], A[i], B[i]} );
        if(A[i] > B[i]) ta+=N[i];
        else tb += N[i];

    }

    sort(ordem.begin(), ordem.end());
    pa = ta - ta%s;
    pb = tb - tb%s;

    int ans = 0;
    for(int i = 0; i < ordem.size(); i++){
        int q = min(ordem[i].x, pa);
        pa -= q;
        ans += q*ordem[i].a;
        ordem[i].x -= q;
    }
    reverse(ordem.begin(), ordem.end());
    for(int i = 0; i < ordem.size(); i++){
        int q = min(ordem[i].x, pb);
        pb -= q;
        ans += q*ordem[i].b;
        ordem[i].x -= q;
    }

    int opa = 0, opb = 0, ab = 0, ba = 0;
    for(int i = 0; i < ordem.size(); i++){
        opa += ordem[i].x*ordem[i].a;
        opb += ordem[i].x*ordem[i].b;

        int q = min(s, ordem[i].x);
        ordem[i].x-=q;
        s-=q;
        ab += q*ordem[i].a;
        ba += q*ordem[i].b;

        ba += ordem[i].x*ordem[i].a;
        ab += ordem[i].x*ordem[i].b;

    }
    cout << ans + max(opa, max(opb, max(ba,ab) )) << endl;
}


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

//tipos
#define int long long
#define ll long long
#define ull unsigned long long
#define ii pair< int , int>
#define vi vector<int>
#define vii vector< pair<int,int> >

//comandos
#define pb push_back
#define mp make_pair
#define sz(x) ((int)x.size())
#define ff first
#define ss second

//constantes
#define MOD 1000000007
#define MAX 31625
#define oo 100000000000005LL

int n, s, k, total;
int N[100005], A[100005], B[100005];

struct doido{
    int x, a, b;

    bool operator < (doido j) const{
        return (a-b) > (j.a-j.b);
    }
};

vector<doido> ordem;

int F(int a){
    int pa = a*s;
    int pb = (k-a)*s;
    int ans =0;

    for(int i = 0; i < ordem.size(); i++){

        int ca = min(pa, ordem[i].x);
        ans += ordem[i].a*ca;
        pa -= ca;

        if(ca < ordem[i].x){
            int cb = min(pb, ordem[i].x - ca);
            ans += ordem[i].b*cb;
            pb -= cb;
        }
    }
    return ans;

}

main(){

    scanf("%lld %lld", &n, &s);
    for(int i = 0; i < n; i++){
        scanf("%lld %lld %lld", &N[i], &A[i], &B[i]);
        total += N[i];
        ordem.push_back( {N[i], A[i], B[i]} );

    }
    sort(ordem.begin(), ordem.end());

    k = (total/s) + (total%s?1:0);

    int e = 0, d = k;

    while(e < d){
        int m2 = (e+d)/2;
        int m1 = (e+m2)/2;
        int m3 = (m2+d)/2;
        if( m3 < d ) m3++;

        int f1 = F(m1);
        int f2 = F(m2);
        int f3 = F(m3);
        /*
        printf("-----\n");
        printf("%lld - %lld\n", m1, f1);
        printf("%lld - %lld\n", m2, f2);
        printf("%lld - %lld\n", m3, f3);
        printf("----\n");
        */
        if(f2 >= f1 and f2 >= f3){
            e = min(m1+1, m2);
            d = max(m3-1, m2);
        }else if ( f1 >= f2 and f1 >= f3 ){
            d = m2-1;
        }else {
            e = m2+1;

        }
    }

    printf("%lld\n", F(e));

}


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

//tipos
#define int long long
#define ll long long
#define ull unsigned long long
#define ii pair< int , int>
#define vi vector<int>
#define vii vector< pair<int,int> >

//comandos
#define pb push_back
#define mp make_pair
#define sz(x) ((int)x.size())
#define ff first
#define ss second

//constantes
#define MOD 1000000007
#define MAX 31625
#define oo 100000000000005LL

int n, s, k, total;
int N[100005], A[100005], B[100005];

struct doido{
    int x, a, b;

    bool operator < (doido j) const{
        return (a-b) > (j.a-j.b);
    }
};

vector<doido> ordem;
int ta, tb, pa, pb;

main(){

    scanf("%lld %lld", &n, &s);
    for(int i = 0; i < n; i++){
        scanf("%lld %lld %lld", &N[i], &A[i], &B[i]);
        total += N[i];
        ordem.push_back( {N[i], A[i], B[i]} );
        if(A[i] > B[i]) ta+=N[i];
        else if(B[i] > A[i] )tb += N[i];
    }

    sort(ordem.begin(), ordem.end());
    pa = ta - ta%s;
    pb = tb - tb%s;
    k = (total/s) + (total%s?1:0);

    int ans = 0;
    for(int i = 0; i < ordem.size(); i++){
        int q = min(ordem[i].x, pa);
        pa -= q;
        ans += q*ordem[i].a;
        ordem[i].x -= q;
    }
    reverse(ordem.begin(), ordem.end());
    for(int i = 0; i < ordem.size(); i++){
        int q = min(ordem[i].x, pb);
        pb -= q;
        ans += q*ordem[i].b;
        ordem[i].x -= q;
    }

    int opa = 0, opb = 0;
    for(int i = 0; i < ordem.size(); i++){
        opa += ordem[i].x*ordem[i].a;
        opb += ordem[i].x*ordem[i].b;
    }
    cout << ans + max(opa, opb) << endl;
}


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

//tipos
#define int long long
#define ll long long
#define ull unsigned long long
#define ii pair< int , int>
#define vi vector<int>
#define vii vector< pair<int,int> >

//comandos
#define pb push_back
#define mp make_pair
#define sz(x) ((int)x.size())
#define ff first
#define ss second

//constantes
#define MOD 1000000007
#define MAX 31625
#define oo 100000000000005LL

int n, s, k, total;
int N[100005], A[100005], B[100005];

struct doido{
    int x, a, b;

    bool operator < (doido j) const{
        return (a-b) > (j.a-j.b);
    }
};

vector<doido> ordem;

int F(int a){
    int pa = a*s;
    int pb = (k-a)*s;
    int ans =0;

    for(int i = 0; i < ordem.size(); i++){

        int ca = min(pa, ordem[i].x);
        ans += ordem[i].a*ca;
        pa -= ca;

        if(ca < ordem[i].x){
            int cb = min(pb, ordem[i].x - ca);
            ans += ordem[i].b*cb;
            pb -= cb;
        }

    }

    pa = a*s;
    pb = (k-a)*s;
    int ans2 =0;

    for(int i = ordem.size()-1; i>= 0; i--){

        int cb = min(pb, ordem[i].x);
        ans2 += ordem[i].b*cb;
        pb -= cb;

        if(cb < ordem[i].x){
            int ca = min(pa, ordem[i].x - cb);
            ans2 += ordem[i].a*ca;
            pa -= ca;
        }
    }

    return max(ans, ans2);

}

main(){

    scanf("%lld %lld", &n, &s);
    for(int i = 0; i < n; i++){
        scanf("%lld %lld %lld", &N[i], &A[i], &B[i]);
        total += N[i];
        ordem.push_back( {N[i], A[i], B[i]} );

    }
    sort(ordem.begin(), ordem.end());

    k = (total/s) + (total%s?1:0);

    int e = 0, d = k;

    while(e < d){
        int m2 = (e+d)/2;
        int m1 = (e+m2)/2;
        int m3 = (m2+d)/2;
        if( m3 < d ) m3++;

        int f1 = F(m1);
        int f2 = F(m2);
        int f3 = F(m3);

        if(f2 >= f1 and f2 >= f3){
            e = min(m1+1, m2);
            d = max(m3-1, m2);
        }else if ( f1 >= f2 and f1 >= f3 ){
            d = m2-1;
        }else {
            e = m2+1;

        }
    }

    printf("%lld\n", F(e));

}


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

//tipos
#define int long long
#define ll long long
#define ull unsigned long long
#define ii pair< int , int>
#define vi vector<int>
#define vii vector< pair<int,int> >

//comandos
#define pb push_back
#define mp make_pair
#define sz(x) ((int)x.size())
#define ff first
#define ss second

//constantes
#define MOD 1000000007
#define MAX 31625
#define oo 100000000000005LL

int n, s, k, total;
int N[100005], A[100005], B[100005];

struct doido{
    int x, a, b;

    bool operator < (doido j) const{
        return (a-b) > (j.a-j.b);
    }
};

vector<doido> ordem;
int ta, tb, pa, pb, te, pe;

main(){

    scanf("%lld %lld", &n, &s);
    for(int i = 0; i < n; i++){
        scanf("%lld %lld %lld", &N[i], &A[i], &B[i]);
        total += N[i];
        ordem.push_back( {N[i], A[i], B[i]} );
        if(A[i] > B[i]) ta+=N[i];
        else if(B[i] > A[i]) tb += N[i];

    }

    sort(ordem.begin(), ordem.end());
    pa = ta - ta%s;
    pb = tb - tb%s;

    int ans = 0;
    for(int i = 0; i < ordem.size(); i++){
        int q = min(ordem[i].x, pa);
        pa -= q;
        ans += q*ordem[i].a;
        ordem[i].x -= q;
    }
    reverse(ordem.begin(), ordem.end());
    for(int i = 0; i < ordem.size(); i++){
        int q = min(ordem[i].x, pb);
        pb -= q;
        ans += q*ordem[i].b;
        ordem[i].x -= q;
    }

    int opa = 0, opb = 0, ab = 0, ba = 0;
    for(int i = 0; i < ordem.size(); i++){
        opa += ordem[i].x*ordem[i].a;
        opb += ordem[i].x*ordem[i].b;

        int q = min(s, ordem[i].x);
        ordem[i].x-=q;
        ab += q*ordem[i].a;
        ba += q*ordem[i].b;

        ba += ordem[i].x*ordem[i].a;
        ab += ordem[i].x*ordem[i].b;

    }
    cout << ans + max(opa, max(opb, max(ba,ab) )) << endl;
}


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

//tipos
#define int long long
#define ll long long
#define ull unsigned long long
#define ii pair< int , int>
#define vi vector<int>
#define vii vector< pair<int,int> >

//comandos
#define pb push_back
#define mp make_pair
#define sz(x) ((int)x.size())
#define ff first
#define ss second

//constantes
#define MOD 1000000007
#define MAX 31625
#define oo 100000000000005LL

int n, s, k, total;
int N[100005], A[100005], B[100005];

struct doido{
    int x, a, b;

    bool operator < (doido j) const{
        return (a-b) > (j.a-j.b);
    }
};

vector<doido> ordem;

int F(int a){
    int pa = a*s;
    int pb = (k-a)*s;
    int ans =0;

    for(int i = 0; i < ordem.size(); i++){

        int ca = min(pa, ordem[i].x);
        ans += ordem[i].a*ca;
        pa -= ca;

        if(ca < ordem[i].x){
            int cb = min(pb, ordem[i].x - ca);
            ans += ordem[i].b*cb;
            pb -= cb;
        }
    }
    return ans;

}

main(){

    scanf("%lld %lld", &n, &s);
    for(int i = 0; i < n; i++){
        scanf("%lld %lld %lld", &N[i], &A[i], &B[i]);
        total += N[i];
        ordem.push_back( {N[i], A[i], B[i]} );

    }
    sort(ordem.begin(), ordem.end());

    k = (total/s) + (total%s?1:0);

    int e = 0, d = k;

    while(e < d){
        int m2 = (e+d)/2;
        int m1 = (e+m2)/2;
        int m3 = (m2+d)/2;
        if( m3 < d) m3++;

        int f1 = F(m1);
        int f2 = F(m2);
        int f3 = F(m3);
        /*
        printf("-----\n");
        printf("%lld - %lld\n", m1, f1);
        printf("%lld - %lld\n", m2, f2);
        printf("%lld - %lld\n", m3, f3);
        printf("----\n");
        */
        if(f2 >= f1 and f2 >= f3){
            if(m2 == d or f2 > F(m2+1)){
                e = d = m2;
            }else{
                e = m1;
                d = m3;
            }
        }else if ( f1 >= f2 and f1 >= f3 ){
            if(m1 == d or f1 > F(m1+1)){
                e = d = m1;
            }else{
                d = m2;
            }
        }else {
            if(m3 == d or f3 > F(m3+1)){
                e = d = m3;
            }else{
                e = m2;
            }
        }
    }

    printf("%lld\n", F(e));

}


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

//tipos
#define int long long
#define ll long long
#define ull unsigned long long
#define ii pair< int , int>
#define vi vector<int>
#define vii vector< pair<int,int> >

//comandos
#define pb push_back
#define mp make_pair
#define sz(x) ((int)x.size())
#define ff first
#define ss second

//constantes
#define MOD 1000000007
#define MAX 31625
#define oo 100000000000005LL

int n, s, k, total;
int N[100005], A[100005], B[100005];

struct doido{
    int x, a, b;

    bool operator < (doido j) const{
        return (a-b) > (j.a-j.b);
    }
};

vector<doido> ordem;
int ta, tb, pa, pb, te, pe;

main(){

    scanf("%lld %lld", &n, &s);
    for(int i = 0; i < n; i++){
        scanf("%lld %lld %lld", &N[i], &A[i], &B[i]);
        total += N[i];
        ordem.push_back( {N[i], A[i], B[i]} );
        if(A[i] > B[i]) ta+=N[i];
        else if(B[i] > A[i] )tb += N[i];
        else te += N[i];
    }

    if(ta%s > tb%s and ta%s){
        int q = min(s-ta%s, te);
        ta += q;
        te -= q;
    }else if(tb%s){
        int q = min(s-tb%s, te);
        tb += q;
        te -= q;
    }

    if(ta%s > tb%s and ta%s){
        int q = min(s-ta%s, te);
        ta += q;
        te -= q;
    }else if(tb%s){
        int q = min(s-tb%s, te);
        tb += q;
        te -= q;
    }

    sort(ordem.begin(), ordem.end());
    pa = ta - ta%s;
    pb = tb - tb%s;
    k = (total/s) + (total%s?1:0);

    int ans = 0;
    for(int i = 0; i < ordem.size(); i++){
        int q = min(ordem[i].x, pa);
        pa -= q;
        ans += q*ordem[i].a;
        ordem[i].x -= q;
    }
    reverse(ordem.begin(), ordem.end());
    for(int i = 0; i < ordem.size(); i++){
        int q = min(ordem[i].x, pb);
        pb -= q;
        ans += q*ordem[i].b;
        ordem[i].x -= q;
    }

    int opa = 0, opb = 0;
    for(int i = 0; i < ordem.size(); i++){
        opa += ordem[i].x*ordem[i].a;
        opb += ordem[i].x*ordem[i].b;
    }
    cout << ans + max(opa, opb) << endl;
}


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

//tipos
#define int long long
#define ll long long
#define ull unsigned long long
#define ii pair< int , int>
#define vi vector<int>
#define vii vector< pair<int,int> >

//comandos
#define pb push_back
#define mp make_pair
#define sz(x) ((int)x.size())
#define ff first
#define ss second

//constantes
#define MOD 1000000007
#define MAX 31625
#define oo 100000000000005LL

int n, s, k, total;
int N[100005], A[100005], B[100005];

struct doido{
    int x, a, b;

    bool operator < (doido j) const{
        return (a-b) > (j.a-j.b);
    }
};

vector<doido> ordem;
int ta, tb, pa, pb;

main(){

    scanf("%lld %lld", &n, &s);
    for(int i = 0; i < n; i++){
        scanf("%lld %lld %lld", &N[i], &A[i], &B[i]);
        total += N[i];
        ordem.push_back( {N[i], A[i], B[i]} );
        if(A[i] >= B[i]) ta+=N[i];
        else if(B[i] > A[i] )tb += N[i];
    }

    sort(ordem.begin(), ordem.end());
    pa = ta - ta%s;
    pb = tb - tb%s;
    k = (total/s) + (total%s?1:0);

    int ans = 0;
    for(int i = 0; i < ordem.size(); i++){
        int q = min(ordem[i].x, pa);
        pa -= q;
        ans += q*ordem[i].a;
        ordem[i].x -= q;
    }
    reverse(ordem.begin(), ordem.end());
    for(int i = 0; i < ordem.size(); i++){
        int q = min(ordem[i].x, pb);
        pb -= q;
        ans += q*ordem[i].b;
        ordem[i].x -= q;
    }

    int opa = 0, opb = 0;
    for(int i = 0; i < ordem.size(); i++){
        opa += ordem[i].x*ordem[i].a;
        opb += ordem[i].x*ordem[i].b;
    }
    cout << ans + max(opa, opb) << endl;
}


#include<bits/stdc++.h>
using namespace std;
int n, s, p[123456], a[123456], b[123456], u=0;
long long tot=0, pa=0, pb=0, pm=0, pi, pia, pib, res=0, r, da=0, db=0;
vector<pair<int, int>> va, vb;
int main () {
    cin >> n >> s;
    for (int i=0; i<n; i++) {
        cin >> p[i] >> a[i] >> b[i];
        tot+=p[i];
        if (a[i]>b[i]) {
                pa+=p[i];
                va.push_back(make_pair(a[i]-b[i], i));
        }
        else if (a[i]==b[i]) continue;
        else {
                pb+=p[i];
                vb.push_back(make_pair(b[i]-a[i], i));
        }
    }
    for (int i=0; i<n; i++) {
        res+=p[i]*max(a[i], b[i]);
    }
    pi=(tot+s-1)/s;
    pia=(pa+s-1)/s;
    pib=(pb+s-1)/s;
    if (pia+pib>pi) {
        sort(va.begin(), va.end());
        sort(vb.begin(), vb.end());
        /*for (int i=0; i<va.size(); i++) cout << va[i].first << " " << p[va[i].second] << " ";
        cout << endl;
        for (int i=0; i<vb.size(); i++) cout << vb[i].first << " " << p[vb[i].second] << " ";
        cout << endl;*/
        pia--;
        if(pia>0) {
            r=pa-s*pia;
            while (r>0) {
                if (r>p[va[u].second]) {
                    r-=p[va[u].second];
                    da+=p[va[u].second]*va[u].first;
                }
                else {
                    da+=va[u].first*r;
                    break;
                }
                u++;
            }
        }
        else if (pia==0) {
            while(true) tot++;
        }
        pia++;
        pib--;
        u=0;
        if(pib>0) {
            r=pb-s*pib;
            while (r>0) {
                if (r>p[vb[u].second]) {
                    r-=p[vb[u].second];
                    db+=p[vb[u].second]*vb[u].first;
                }
                else {
                    db+=vb[u].first*r;
                    break;
                }
                u++;
            }
        }
        else if (pib==0) {
            while (true) tot++;
        }
        res-=min(da, db);
    }
    cout << res << endl;
}


#include<bits/stdc++.h>
using namespace std;
long long tot=0, pa=0, pb=0, pm=0, pi, pia, pib, res=0, r, da=0, db=0, n, s, p[123456], a[123456], b[123456], u=0;
vector<pair<long long, long long>> va, vb;
int main () {
    cin >> n >> s;
    for (int i=0; i<n; i++) {
        cin >> p[i] >> a[i] >> b[i];
        tot+=p[i];
        if (a[i]>b[i]) {
                pa+=p[i];
                va.push_back(make_pair(a[i]-b[i], i));
        }
        else if (a[i]==b[i]) continue;
        else {
                pb+=p[i];
                vb.push_back(make_pair(b[i]-a[i], i));
        }
    }
    for (int i=0; i<n; i++) {
        res+=p[i]*max(a[i], b[i]);
    }
    pi=(tot+s-1)/s;
    pia=(pa+s-1)/s;
    pib=(pb+s-1)/s;
    if (pia+pib>pi) {
        sort(va.begin(), va.end());
        sort(vb.begin(), vb.end());
        /*for (int i=0; i<va.size(); i++) cout << va[i].first << " " << p[va[i].second] << " ";
        cout << endl;
        for (int i=0; i<vb.size(); i++) cout << vb[i].first << " " << p[vb[i].second] << " ";
        cout << endl;*/
        pia--;
        if(pia>=0) {
            r=pa-s*pia;
            while (r>0 && u<va.size()) {
                if (r>p[va[u].second]) {
                    r-=p[va[u].second];
                    da+=p[va[u].second]*va[u].first;
                }
                else {
                    da+=va[u].first*r;
                    break;
                }
                u++;
            }
        }
        pia++;
        pib--;
        u=0;
        if(pib>=0) {
            r=pb-s*pib;
            while (r>0 && u<vb.size()) {
                if (r>p[vb[u].second]) {
                    r-=p[vb[u].second];
                    db+=p[vb[u].second]*vb[u].first;
                }
                else {
                    db+=vb[u].first*r;
                    break;
                }
                u++;
            }
        }
        res-=min(da, db);
    }
    cout << res << endl;
}


#include<bits/stdc++.h>
using namespace std;
int n, s, p[123456], a[123456], b[123456], u=0;
long long tot=0, pa=0, pb=0, pi, pia, pib, res=0, r, da=0, db=0;
vector<pair<int, int>> va, vb;
int main () {
    cin >> n >> s;
    for (int i=0; i<n; i++) {
        cin >> p[i] >> a[i] >> b[i];
        tot+=p[i];
        if (a[i]>=b[i]) {
                pa+=p[i];
                va.push_back(make_pair(a[i]-b[i], i));
        }
        else {
                pb+=p[i];
                vb.push_back(make_pair(b[i]-a[i], i));
        }
    }
    for (int i=0; i<n; i++) {
        res+=p[i]*max(a[i], b[i]);
    }
    pi=(tot+s-1)/s;
    pia=(pa+s-1)/s;
    pib=(pb+s-1)/s;
    if (pia+pib>pi) {
        sort(va.begin(), va.end());
        sort(vb.begin(), vb.end());
        /*for (int i=0; i<va.size(); i++) cout << va[i].first << " " << p[va[i].second] << " ";
        cout << endl;
        for (int i=0; i<vb.size(); i++) cout << vb[i].first << " " << p[vb[i].second] << " ";
        cout << endl;*/
        pia--;
        //if(pia>=0) {
            r=pa-s*pia;
            while (r>0) {
                if (r>p[va[u].second]) {
                    r-=p[va[u].second];
                    da+=p[va[u].second]*va[u].first;
                }
                else {
                    da+=va[u].first*r;
                    break;
                }
                u++;
            }
        //}
        pia++;
        pib--;
        u=0;
        //if(pib>=0) {
            r=pb-s*pib;
            while (r>0) {
                if (r>p[vb[u].second]) {
                    r-=p[vb[u].second];
                    db+=p[vb[u].second]*vb[u].first;
                }
                else {
                    db+=vb[u].first*r;
                    break;
                }
                u++;
            }
        //}
        res-=min(da, db);
    }
    cout << res << endl;
}


#include<bits/stdc++.h>
using namespace std;
int n, s, p[123456], a[123456], b[123456], u=0;
long long tot=0, pa=0, pb=0, pm=0, pi, pia, pib, res=0, r, da=0, db=0;
vector<pair<int, int>> va, vb;
int main () {
    cin >> n >> s;
    for (int i=0; i<n; i++) {
        cin >> p[i] >> a[i] >> b[i];
        tot+=p[i];
        if (a[i]>b[i]) {
                pa+=p[i];
                va.push_back(make_pair(a[i]-b[i], i));
        }
        else if (a[i]==b[i]) continue;
        else {
                pb+=p[i];
                vb.push_back(make_pair(b[i]-a[i], i));
        }
    }
    for (int i=0; i<n; i++) {
        res+=p[i]*max(a[i], b[i]);
    }
    pi=(tot+s-1)/s;
    pia=(pa+s-1)/s;
    pib=(pb+s-1)/s;
    if (pia+pib>pi) {
        sort(va.begin(), va.end());
        sort(vb.begin(), vb.end());
        /*for (int i=0; i<va.size(); i++) cout << va[i].first << " " << p[va[i].second] << " ";
        cout << endl;
        for (int i=0; i<vb.size(); i++) cout << vb[i].first << " " << p[vb[i].second] << " ";
        cout << endl;*/
        pia--;
        if(pia>0) {
            r=pa-s*pia;
            while (r>0) {
                if (r>p[va[u].second]) {
                    r-=p[va[u].second];
                    da+=p[va[u].second]*va[u].first;
                }
                else {
                    da+=va[u].first*r;
                    break;
                }
                u++;
            }
        }
        else if (pia==0) {
            for (int i=0; i<va.size(); i++) da+=p[va[i].second]*va[i].first;
        }
        pia++;
        pib--;
        u=0;
        if(pib>0) {
            r=pb-s*pib;
            while (r>0) {
                if (r>p[vb[u].second]) {
                    r-=p[vb[u].second];
                    db+=p[vb[u].second]*vb[u].first;
                }
                else {
                    db+=vb[u].first*r;
                    break;
                }
                u++;
            }
        }
        else if (pib==0) {
            for (int i=0; i<vb.size(); i++) db+=p[vb[i].second]*vb[i].first;
        }
        res-=min(da, db);
    }
    cout << res << endl;
}


#include<bits/stdc++.h>
using namespace std;
int n, s, p[123456], a[123456], b[123456], u=0;
long long tot=0, pa=0, pb=0, pm=0, pi, pia, pib, res=0, r, da=0, db=0;
vector<pair<int, int>> va, vb;
int main () {
    cin >> n >> s;
    for (int i=0; i<n; i++) {
        cin >> p[i] >> a[i] >> b[i];
        tot+=p[i];
        if (a[i]>b[i]) {
                pa+=p[i];
                va.push_back(make_pair(a[i]-b[i], i));
        }
        else if (a[i]==b[i]) continue;
        else {
                pb+=p[i];
                vb.push_back(make_pair(b[i]-a[i], i));
        }
    }
    for (int i=0; i<n; i++) {
        res+=p[i]*max(a[i], b[i]);
    }
    pi=(tot+s-1)/s;
    pia=(pa+s-1)/s;
    pib=(pb+s-1)/s;
    if (pia+pib>pi) {
        sort(va.begin(), va.end());
        sort(vb.begin(), vb.end());
        /*for (int i=0; i<va.size(); i++) cout << va[i].first << " " << p[va[i].second] << " ";
        cout << endl;
        for (int i=0; i<vb.size(); i++) cout << vb[i].first << " " << p[vb[i].second] << " ";
        cout << endl;*/
        pia--;
        if(pia>0) {
            r=pa-s*pia;
            while (r>0 && u<va.size()) {
                if (r>p[va[u].second]) {
                    r-=p[va[u].second];
                    da+=p[va[u].second]*va[u].first;
                }
                else {
                    da+=va[u].first*r;
                    break;
                }
                u++;
            }
        }
        else if (pia==0) {
            for (int i=0; i<va.size(); i++) da+=p[va[i].second]*va[i].first;
        }
        pia++;
        pib--;
        u=0;
        if(pib>=0) {
            r=pb-s*pib;
            while (r>0 && u<vb.size()) {
                if (r>p[vb[u].second]) {
                    r-=p[vb[u].second];
                    db+=p[vb[u].second]*vb[u].first;
                }
                else {
                    db+=vb[u].first*r;
                    break;
                }
                u++;
            }
        }
        else if (pib==0) {
            for (int i=0; i<vb.size(); i++) db+=p[vb[i].second]*vb[i].first;
        }
        res-=min(da, db);
    }
    cout << res << endl;
}


#include<bits/stdc++.h>
using namespace std;
int n, s, p[123456], a[123456], b[123456], u=0;
long long tot=0, pa=0, pb=0, pm=0, pi, pia, pib, res=0, r, da=0, db=0;
vector<pair<int, int>> va, vb;
int main () {
    cin >> n >> s;
    for (int i=0; i<n; i++) {
        cin >> p[i] >> a[i] >> b[i];
        tot+=p[i];
        if (a[i]>b[i]) {
                pa+=p[i];
                va.push_back(make_pair(a[i]-b[i], i));
        }
        else if (a[i]==b[i]) continue;
        else {
                pb+=p[i];
                vb.push_back(make_pair(b[i]-a[i], i));
        }
    }
    for (int i=0; i<n; i++) {
        res+=p[i]*max(a[i], b[i]);
    }
    pi=(tot+s-1)/s;
    pia=(pa+s-1)/s;
    pib=(pb+s-1)/s;
    if (pia+pib>pi) {
        sort(va.begin(), va.end());
        sort(vb.begin(), vb.end());
        /*for (int i=0; i<va.size(); i++) cout << va[i].first << " " << p[va[i].second] << " ";
        cout << endl;
        for (int i=0; i<vb.size(); i++) cout << vb[i].first << " " << p[vb[i].second] << " ";
        cout << endl;*/
        pia--;
        if(pia>=0) {
            r=pa-s*pia;
            while (r>0) {
                if (r>p[va[u].second]) {
                    r-=p[va[u].second];
                    da+=p[va[u].second]*va[u].first;
                }
                else {
                    da+=va[u].first*r;
                    break;
                }
                u++;
            }
        }
        pia++;
        pib--;
        u=0;
        if(pib>=0) {
            r=pb-s*pib;
            while (r>0) {
                if (r>p[vb[u].second]) {
                    r-=p[vb[u].second];
                    db+=p[vb[u].second]*vb[u].first;
                }
                else {
                    db+=vb[u].first*r;
                    break;
                }
                u++;
            }
        }
        res-=min(da, db);
    }
    cout << res << endl;
}


#include<bits/stdc++.h>
using namespace std;
int n, s, p[123456], a[123456], b[123456], t=0, u=0;
long long tot=0, pa=0, pb=0, pi, pia, pib, res=0, r, da=0, db=0;
vector<pair<int, int>> va, vb;
int main () {
    cin >> n >> s;
    for (int i=0; i<n; i++) {
        cin >> p[i] >> a[i] >> b[i];
        tot+=p[i];
        if (a[i]>=b[i]) {
                pa+=p[i];
                va.push_back(make_pair(a[i]-b[i], i));
        }
        else {
                pb+=p[i];
                vb.push_back(make_pair(b[i]-a[i], i));
        }
    }
    for (int i=0; i<n; i++) {
        res+=p[i]*max(a[i], b[i]);
    }
    pi=(tot+s-1)/s;
    pia=(pa+s-1)/s;
    pib=(pb+s-1)/s;
    if (pia+pib>pi) {
        sort(va.begin(), va.end());
        sort(vb.begin(), vb.end());
        pia--;
        if(pia>=0) {
            r=pa-s*pia;
            while (t<r) {
                if (r-t>p[va[u].second]) {
                    t+=p[va[u].second];
                    da+=p[va[u].second]*va[u].first;
                }
                else {
                    da+=va[u].first*(r-t);
                    break;
                }
                u++;
            }
        }
        pia++;
        pib--;
        t=0;
        u=0;
        if(pib>=0) {
            r=pb-s*pib;
            while (t<r) {
                if (r-t>p[vb[u].second]) {
                    t+=p[vb[u].second];
                    db+=p[vb[u].second]*vb[u].first;
                }
                else {
                    db+=vb[u].first*(r-t);
                    break;
                }
                u++;
            }
        }
        res-=min(da, db);
    }
    cout << res << endl;
}


/*
ID: kfoozminus
TASK:
LANG: C++
*/

#include <bits/stdc++.h>
using namespace std;
//typedefs
/*
typedef long long ll;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <int, pii> piii;
typedef vector <int> vi;
typedef vector <ll> vl;
typedef pair <ll, ll> pll;
const double PI = acos(-1);
*/
//typedefs
#define ll long long
#define ull unsigned long long
#define pii pair <int, int>
#define piii pair <int, pii>
#define vi vector <int>
#define vl vector <ll>
#define pll pair <ll, ll>
#define PI acos(-1)
//defines
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define mem(a, b) memset(a, b, sizeof(a))
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define SQR(a) ((a)*(a))
#define inf 100000000
#define mod 1000000007
#define mod1 1000000007
#define mod2 1000000009
#define b1 43
#define b2 41
#define EPS 1e-9
//define harmonic(n) 0.57721566490153286l+log(n)
#define nl puts("")
#define odd(n) ((n)&1)
#define even(n) (!((n)&1))
#define vsort(v) sort(v.begin(), v.end())
#define lc (node<<1)
#define rc ((node<<1)|1)
#define ran(a, b) ((((rand() << 15) ^ rand()) % ((b) - (a) + 1)) + (a))
//loop
#define rep(i, n) for(int i = 0; i < n; ++i)
#define REP(i, n) for(int i = 1; i <= n; ++i)
//input
#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d%d", &a, &b)
#define siii(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define sl(a) scanf("%lld", &a)
#define sll(a, b) scanf("%lld%lld", &a, &b)
#define slll(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)
#define sd(a) scanf("%lf", &a)
#define sc(a) scanf("%c", &a)
#define sst(a) scanf("%s", a)
inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
//debug
#ifdef kfoozminus

template < typename F, typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
	return os << "(" << p.first << ", " << p.second << ")";
}

template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
	os << "{";
	for(auto it = v.begin(); it != v.end(); ++it) {
		if( it != v.begin() ) os << ", ";
		os << *it;
	}
    return os << "}";
}

template < typename T >
ostream &operator << ( ostream & os, const set< T > &v ) {
	os << "[";
	for(auto it = v.begin(); it != v.end(); ++it) {
		if( it != v.begin()) os << ", ";
		os << *it;
	}
    return os << "]";
}

template < typename F, typename S >
ostream &operator << ( ostream & os, const map< F, S > &v ) {
	os << "[";
	for(auto it = v.begin(); it != v.end(); ++it) {
		if( it != v.begin() ) os << ", ";
		os << it -> first << " = " << it -> second ;
	}
    return os << "]";
}

#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)

clock_t tStart = clock();
#define timeStamp dbg("Execution Time: ", (double)(clock() - tStart)/CLOCKS_PER_SEC)

void faltu () { cerr << endl; }

template <typename T>
void faltu( T a[], int n ) {
	for(int i = 0; i < n; ++i) cerr << a[i] << ' ';
	cerr << endl;
}

template <typename T, typename ... hello>
void faltu( T arg, const hello &... rest) { cerr << arg << ' '; faltu(rest...); }

#else
#define dbg(args...)
#endif
/*
ll add(ll a, ll b) {
	ll ret = a+b;
	if(ret >= mod) ret -= mod;
	return ret;
}

ll subtract(ll a, ll b) {
	ll ret = a-b;
	if(ret < 0) ret += mod;
	return ret;
}

ll mult(ll a, ll b) {
	ll ret = a*b;
	if(ret >= mod) ret %= mod;
	return ret;
}

ll bigmod(ll a, ll b) {
	ll ret = 1;
	while(b) {
		if(b&1) ret = mult(ret, a);
		b >>= 1; a = mult(a, a);
	}
	return ret;
}
*/

//ll inverse(ll n) { return bigmod(n, mod-2); }

/*
#define PMX 1000000

int marked[PMX/64 + 2];
vi primes;

#define mark(x) marked[x >> 6] |= (1 << ((x & 63) >> 1))
#define check(x) (marked[x >> 6] & (1 << ((x & 63) >> 1)))

bool isPrime(int x)
{
	return (x > 1) && ((x == 2) || ((x & 1) && (!(check(x)))));
}

void seive(int n)
{
	int i, j;
	for(i = 3; i * i <= n; i += 2) if(!check(i)) for(j = i * i; j <= n; j += i << 1) mark(j);
	primes.PB(2);
	for(i = 3; i <= n; i += 2) if(isPrime(i)) primes.PB(i);
}
*/

//Direction Array
//int fx[]={1, -1, 0, 0}; int fy[]={0, 0, 1, -1};
//int fx[]={0, 0, 1, -1, -1, 1, -1, 1}; int fy[]={-1, 1, 0, 0, 1, 1, -1, -1};

//bit manipulation
/*
bool checkBit(int n, int i) { return (n&(1<<i)); }
int setBit(int n, int i) { return (n|(1<<i)); }
int resetBit(int n, int i) { return (n&(~(1<<i))); }
*/
//end of template

#define MX 100007

set< pair< pll, int > > s1, s2, c1, c2;
pair< pll, int > v;
ll s[MX], vis[MX], a[MX], b[MX], SS;
int n;

ll f(ll tp1, ll tp2)
{
	int i;
	c1 = s1;
	c2 = s2;
	dbg(c1, c2);
	for(i = 0; i < n; i ++) vis[i] = s[i];

	ll ret = 0;
	ll tot = tp1 * SS;
	ll sum = 0;
	auto it = c1.begin();
	while(sum < tot && it != c1.end()) {
		v = *it;
		ll ad = min(tot - sum, vis[v.S]);
		sum += ad;
		ret += ad * (-v.F.F);
		it ++;
		vis[v.S] -= ad;
	}
	tot = tp2 * SS;
	sum = 0;
	it = c2.begin();
	while(sum < tot && it != c2.end()) {
		v = *it;
		ll ad = min(tot - sum, vis[v.S]);
		sum += ad;
		ret += ad * (-v.F.F);
		it ++;
		vis[v.S] -= ad;
	}
	return ret;
}

int main () {
#ifdef kfoozminus
//	freopen("in", "r", stdin);
//	freopen("out", "w", stdout);
#endif
	int i;
	ll tot, mn, mx, mid1, mid2, ans1, ans2, sum = 0;

	scanf("%d %lld", &n, &SS);
	for(i = 0; i < n; i ++) {
		scanf("%lld %lld %lld", &s[i], &a[i], &b[i]);
		s1.insert( {{-a[i], b[i]}, i} );
		s2.insert( {{-b[i], a[i]}, i} );
		sum += s[i];
	}
	tot = (sum + SS - 1)/SS;

	mn = 0;
	mx = tot;
	while(mn < mx) {
		mid1 = mn + (mx - mn) /3;
		mid2 = mx - (mx - mn) /3;
		dbg(mid1, mid2);

		ans1 = f(mid1, tot - mid1);
		ans2 = f(mid2, tot - mid2);
		dbg(ans1, ans2);

		if(ans1 < ans2) mn = mid1 + 1;
		else mx = mid2 - 1;
	}
	cout << f(mn, tot - mn) << endl;

//	timeStamp;
	return 0;
}


/*
ID: kfoozminus
TASK:
LANG: C++
*/

#include <bits/stdc++.h>
using namespace std;
//typedefs
/*
typedef long long ll;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <int, pii> piii;
typedef vector <int> vi;
typedef vector <ll> vl;
typedef pair <ll, ll> pll;
const double PI = acos(-1);
*/
//typedefs
#define ll long long
#define ull unsigned long long
#define pii pair <int, int>
#define piii pair <int, pii>
#define vi vector <int>
#define vl vector <ll>
#define pll pair <ll, ll>
#define PI acos(-1)
//defines
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define mem(a, b) memset(a, b, sizeof(a))
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define SQR(a) ((a)*(a))
#define inf 100000000
#define mod 1000000007
#define mod1 1000000007
#define mod2 1000000009
#define b1 43
#define b2 41
#define EPS 1e-9
//define harmonic(n) 0.57721566490153286l+log(n)
#define nl puts("")
#define odd(n) ((n)&1)
#define even(n) (!((n)&1))
#define vsort(v) sort(v.begin(), v.end())
#define lc (node<<1)
#define rc ((node<<1)|1)
#define ran(a, b) ((((rand() << 15) ^ rand()) % ((b) - (a) + 1)) + (a))
//loop
#define rep(i, n) for(int i = 0; i < n; ++i)
#define REP(i, n) for(int i = 1; i <= n; ++i)
//input
#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d%d", &a, &b)
#define siii(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define sl(a) scanf("%lld", &a)
#define sll(a, b) scanf("%lld%lld", &a, &b)
#define slll(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)
#define sd(a) scanf("%lf", &a)
#define sc(a) scanf("%c", &a)
#define sst(a) scanf("%s", a)
inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
//debug
#ifdef kfoozminus

template < typename F, typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
	return os << "(" << p.first << ", " << p.second << ")";
}

template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
	os << "{";
	for(auto it = v.begin(); it != v.end(); ++it) {
		if( it != v.begin() ) os << ", ";
		os << *it;
	}
    return os << "}";
}

template < typename T >
ostream &operator << ( ostream & os, const set< T > &v ) {
	os << "[";
	for(auto it = v.begin(); it != v.end(); ++it) {
		if( it != v.begin()) os << ", ";
		os << *it;
	}
    return os << "]";
}

template < typename F, typename S >
ostream &operator << ( ostream & os, const map< F, S > &v ) {
	os << "[";
	for(auto it = v.begin(); it != v.end(); ++it) {
		if( it != v.begin() ) os << ", ";
		os << it -> first << " = " << it -> second ;
	}
    return os << "]";
}

#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)

clock_t tStart = clock();
#define timeStamp dbg("Execution Time: ", (double)(clock() - tStart)/CLOCKS_PER_SEC)

void faltu () { cerr << endl; }

template <typename T>
void faltu( T a[], int n ) {
	for(int i = 0; i < n; ++i) cerr << a[i] << ' ';
	cerr << endl;
}

template <typename T, typename ... hello>
void faltu( T arg, const hello &... rest) { cerr << arg << ' '; faltu(rest...); }

#else
#define dbg(args...)
#endif
/*
ll add(ll a, ll b) {
	ll ret = a+b;
	if(ret >= mod) ret -= mod;
	return ret;
}

ll subtract(ll a, ll b) {
	ll ret = a-b;
	if(ret < 0) ret += mod;
	return ret;
}

ll mult(ll a, ll b) {
	ll ret = a*b;
	if(ret >= mod) ret %= mod;
	return ret;
}

ll bigmod(ll a, ll b) {
	ll ret = 1;
	while(b) {
		if(b&1) ret = mult(ret, a);
		b >>= 1; a = mult(a, a);
	}
	return ret;
}
*/

//ll inverse(ll n) { return bigmod(n, mod-2); }

/*
#define PMX 1000000

int marked[PMX/64 + 2];
vi primes;

#define mark(x) marked[x >> 6] |= (1 << ((x & 63) >> 1))
#define check(x) (marked[x >> 6] & (1 << ((x & 63) >> 1)))

bool isPrime(int x)
{
	return (x > 1) && ((x == 2) || ((x & 1) && (!(check(x)))));
}

void seive(int n)
{
	int i, j;
	for(i = 3; i * i <= n; i += 2) if(!check(i)) for(j = i * i; j <= n; j += i << 1) mark(j);
	primes.PB(2);
	for(i = 3; i <= n; i += 2) if(isPrime(i)) primes.PB(i);
}
*/

//Direction Array
//int fx[]={1, -1, 0, 0}; int fy[]={0, 0, 1, -1};
//int fx[]={0, 0, 1, -1, -1, 1, -1, 1}; int fy[]={-1, 1, 0, 0, 1, 1, -1, -1};

//bit manipulation
/*
bool checkBit(int n, int i) { return (n&(1<<i)); }
int setBit(int n, int i) { return (n|(1<<i)); }
int resetBit(int n, int i) { return (n&(~(1<<i))); }
*/
//end of template

#define MX 100007

set< pair< pll, int > > s1, s2, c1, c2;
pair< pll, int > v;
ll s[MX], vis[MX], a[MX], b[MX], SS;
int n;

ll f(ll tp1, ll tp2)
{
	int i;
	c1 = s1;
	c2 = s2;
	for(i = 0; i < n; i ++) vis[i] = s[i];

	ll ret = 0;
	ll tot = tp1 * SS;
	ll sum = 0;
	auto it = c1.begin();
	while(sum < tot && it != c1.end()) {
		v = *it;
		ll ad = min(tot - sum, vis[v.S]);
		sum += ad;
		ret += ad * (-v.F.F);
		it ++;
		vis[v.S] -= ad;
	}
	tot = tp2 * SS;
	sum = 0;
	it = c2.begin();
	while(sum < tot && it != c2.end()) {
		v = *it;
		ll ad = min(tot - sum, vis[v.S]);
		sum += ad;
		ret += ad * (-v.F.F);
		it ++;
		vis[v.S] -= ad;
	}
	return ret;
}

int main () {
#ifdef kfoozminus
//	freopen("in", "r", stdin);
//	freopen("out", "w", stdout);
#endif
	int i;
	ll tot, mn, mx, mid1, mid2, ans1, ans2, sum = 0, ans = 0;

	scanf("%d %lld", &n, &SS);
	for(i = 0; i < n; i ++) {
		scanf("%lld %lld %lld", &s[i], &a[i], &b[i]);
		s1.insert( {{-a[i], b[i]}, i} );
		s2.insert( {{-b[i], a[i]}, i} );
		sum += s[i];
	}
	tot = (sum + SS - 1)/SS;

	mn = 0;
	mx = tot;
	for(i = 0; i <= tot; i ++) ans = max(ans, f(i, tot - i));
	/*while(mn < mx) {
		mid1 = mn + (mx - mn) /3;
		mid2 = mx - (mx - mn) /3;
		dbg(mid1, mid2);

		ans1 = f(mid1, tot - mid1);
		ans2 = f(mid2, tot - mid2);
		dbg(ans1, ans2);

		if(ans1 < ans2) mn = mid1 + 1;
		else mx = mid2 - 1;
	}
	ans = f(mn, tot - mn);*/
	cout << ans << endl;

//	timeStamp;
	return 0;
}


/*
ID: kfoozminus
TASK:
LANG: C++
*/

#include <bits/stdc++.h>
using namespace std;
//typedefs
/*
typedef long long ll;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <int, pii> piii;
typedef vector <int> vi;
typedef vector <ll> vl;
typedef pair <ll, ll> pll;
const double PI = acos(-1);
*/
//typedefs
#define ll long long
#define ull unsigned long long
#define pii pair <int, int>
#define piii pair <int, pii>
#define vi vector <int>
#define vl vector <ll>
#define pll pair <ll, ll>
#define PI acos(-1)
//defines
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define mem(a, b) memset(a, b, sizeof(a))
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define SQR(a) ((a)*(a))
#define inf 100000000
#define mod 1000000007
#define mod1 1000000007
#define mod2 1000000009
#define b1 43
#define b2 41
#define EPS 1e-9
//define harmonic(n) 0.57721566490153286l+log(n)
#define nl puts("")
#define odd(n) ((n)&1)
#define even(n) (!((n)&1))
#define vsort(v) sort(v.begin(), v.end())
#define lc (node<<1)
#define rc ((node<<1)|1)
#define ran(a, b) ((((rand() << 15) ^ rand()) % ((b) - (a) + 1)) + (a))
//loop
#define rep(i, n) for(int i = 0; i < n; ++i)
#define REP(i, n) for(int i = 1; i <= n; ++i)
//input
#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d%d", &a, &b)
#define siii(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define sl(a) scanf("%lld", &a)
#define sll(a, b) scanf("%lld%lld", &a, &b)
#define slll(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)
#define sd(a) scanf("%lf", &a)
#define sc(a) scanf("%c", &a)
#define sst(a) scanf("%s", a)
inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
//debug
#ifdef kfoozminus

template < typename F, typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
	return os << "(" << p.first << ", " << p.second << ")";
}

template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
	os << "{";
	for(auto it = v.begin(); it != v.end(); ++it) {
		if( it != v.begin() ) os << ", ";
		os << *it;
	}
    return os << "}";
}

template < typename T >
ostream &operator << ( ostream & os, const set< T > &v ) {
	os << "[";
	for(auto it = v.begin(); it != v.end(); ++it) {
		if( it != v.begin()) os << ", ";
		os << *it;
	}
    return os << "]";
}

template < typename F, typename S >
ostream &operator << ( ostream & os, const map< F, S > &v ) {
	os << "[";
	for(auto it = v.begin(); it != v.end(); ++it) {
		if( it != v.begin() ) os << ", ";
		os << it -> first << " = " << it -> second ;
	}
    return os << "]";
}

#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)

clock_t tStart = clock();
#define timeStamp dbg("Execution Time: ", (double)(clock() - tStart)/CLOCKS_PER_SEC)

void faltu () { cerr << endl; }

template <typename T>
void faltu( T a[], int n ) {
	for(int i = 0; i < n; ++i) cerr << a[i] << ' ';
	cerr << endl;
}

template <typename T, typename ... hello>
void faltu( T arg, const hello &... rest) { cerr << arg << ' '; faltu(rest...); }

#else
#define dbg(args...)
#endif
/*
ll add(ll a, ll b) {
	ll ret = a+b;
	if(ret >= mod) ret -= mod;
	return ret;
}

ll subtract(ll a, ll b) {
	ll ret = a-b;
	if(ret < 0) ret += mod;
	return ret;
}

ll mult(ll a, ll b) {
	ll ret = a*b;
	if(ret >= mod) ret %= mod;
	return ret;
}

ll bigmod(ll a, ll b) {
	ll ret = 1;
	while(b) {
		if(b&1) ret = mult(ret, a);
		b >>= 1; a = mult(a, a);
	}
	return ret;
}
*/

//ll inverse(ll n) { return bigmod(n, mod-2); }

/*
#define PMX 1000000

int marked[PMX/64 + 2];
vi primes;

#define mark(x) marked[x >> 6] |= (1 << ((x & 63) >> 1))
#define check(x) (marked[x >> 6] & (1 << ((x & 63) >> 1)))

bool isPrime(int x)
{
	return (x > 1) && ((x == 2) || ((x & 1) && (!(check(x)))));
}

void seive(int n)
{
	int i, j;
	for(i = 3; i * i <= n; i += 2) if(!check(i)) for(j = i * i; j <= n; j += i << 1) mark(j);
	primes.PB(2);
	for(i = 3; i <= n; i += 2) if(isPrime(i)) primes.PB(i);
}
*/

//Direction Array
//int fx[]={1, -1, 0, 0}; int fy[]={0, 0, 1, -1};
//int fx[]={0, 0, 1, -1, -1, 1, -1, 1}; int fy[]={-1, 1, 0, 0, 1, 1, -1, -1};

//bit manipulation
/*
bool checkBit(int n, int i) { return (n&(1<<i)); }
int setBit(int n, int i) { return (n|(1<<i)); }
int resetBit(int n, int i) { return (n&(~(1<<i))); }
*/
//end of template

#define MX 100007

set< pair< pll, int > > s1, s2, c1, c2;
//pair< pll, int > v;
set< pii > st, c;
ll s[MX], vis[MX], a[MX], b[MX], SS;
int n;
ll R;

ll f(ll tp1)
{
	int i;
	c = st;
	//c1 = s1;
	//c2 = s2;
	for(i = 0; i < n; i ++) vis[i] = s[i];

	ll ret = 0;
	ll tot = tp1 * SS;
	ll sum = 0;
	auto it = c.begin();
	if(tot) {
		tot -= R;
		while(sum < tot && it != c.end()) {
			pii v = *it;
			ll ad = min(tot - sum, vis[v.S]);
			sum += ad;
			ret += ad * a[v.S];
			it ++;
			vis[v.S] -= ad;
		}
		if(vis[v.S]) it --;
		tot += R;
	}
	for(i = 0; i < n; i ++) ret += vis[i] * b[i];
	ll ans = ret;
	while(sum < tot && it != c.end()) {
		pii v = *it;
		sum ++;
		ret += a[v.S];
		ret -= b[v.S];
		vis[v.S] --;
		if(vis[v.S] == 0) it ++;
		ans = max(ans, ret);
	}

	/*ll ret = 0;
	ll tot = tp1 * SS;
	ll sum = 0;
	auto it = c1.begin();
	while(sum < tot && it != c1.end()) {
		v = *it;
		ll ad = min(tot - sum, vis[v.S]);
		sum += ad;
		ret += ad * a[v.S];
		it ++;
		vis[v.S] -= ad;
	}
	tot = tp2 * SS;
	sum = 0;
	it = c2.begin();
	while(sum < tot && it != c2.end()) {
		v = *it;
		ll ad = min(tot - sum, vis[v.S]);
		sum += ad;
		ret += ad * b[v.S];
		it ++;
		vis[v.S] -= ad;
	}*/
	return ans;
}

int main () {
#ifdef kfoozminus
//	freopen("in", "r", stdin);
//	freopen("out", "w", stdout);
#endif
	int i;
	ll tot, mn, mx, mid1, mid2, ans1, ans2, sum = 0, ans = 0;

	scanf("%d %lld", &n, &SS);
	for(i = 0; i < n; i ++) {
		scanf("%lld %lld %lld", &s[i], &a[i], &b[i]);
		st.insert( {-(a[i] - b[i]), i} );
		//s2.insert( {{-(b[i] - a[i]), -b[i]}, i} );
		sum += s[i];
	}
	tot = (sum + SS - 1)/SS;
	R = tot * SS - sum;

	mn = 0;
	mx = tot;
	//for(i = 0; i <= tot; i ++) ans = max(ans, f(i, tot - i));
	while(mn < mx) {
		mid1 = mn + (mx - mn) /3;
		mid2 = mx - (mx - mn) /3;
		dbg(mid1, mid2);

		ans1 = f(mid1);
		ans2 = f(mid2);
		dbg(ans1, ans2);

		if(ans1 < ans2) mn = mid1 + 1;
		else mx = mid2 - 1;
	}
	cout << f(mn) << endl;

//	timeStamp;
	return 0;
}


/*
ID: kfoozminus
TASK:
LANG: C++
*/

#include <bits/stdc++.h>
using namespace std;
//typedefs
/*
typedef long long ll;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <int, pii> piii;
typedef vector <int> vi;
typedef vector <ll> vl;
typedef pair <ll, ll> pll;
const double PI = acos(-1);
*/
//typedefs
#define ll long long
#define ull unsigned long long
#define pii pair <int, int>
#define piii pair <int, pii>
#define vi vector <int>
#define vl vector <ll>
#define pll pair <ll, ll>
#define PI acos(-1)
//defines
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define mem(a, b) memset(a, b, sizeof(a))
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define SQR(a) ((a)*(a))
#define inf 100000000
#define mod 1000000007
#define mod1 1000000007
#define mod2 1000000009
#define b1 43
#define b2 41
#define EPS 1e-9
//define harmonic(n) 0.57721566490153286l+log(n)
#define nl puts("")
#define odd(n) ((n)&1)
#define even(n) (!((n)&1))
#define vsort(v) sort(v.begin(), v.end())
#define lc (node<<1)
#define rc ((node<<1)|1)
#define ran(a, b) ((((rand() << 15) ^ rand()) % ((b) - (a) + 1)) + (a))
//loop
#define rep(i, n) for(int i = 0; i < n; ++i)
#define REP(i, n) for(int i = 1; i <= n; ++i)
//input
#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d%d", &a, &b)
#define siii(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define sl(a) scanf("%lld", &a)
#define sll(a, b) scanf("%lld%lld", &a, &b)
#define slll(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)
#define sd(a) scanf("%lf", &a)
#define sc(a) scanf("%c", &a)
#define sst(a) scanf("%s", a)
inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
//debug
#ifdef kfoozminus

template < typename F, typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
	return os << "(" << p.first << ", " << p.second << ")";
}

template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
	os << "{";
	for(auto it = v.begin(); it != v.end(); ++it) {
		if( it != v.begin() ) os << ", ";
		os << *it;
	}
    return os << "}";
}

template < typename T >
ostream &operator << ( ostream & os, const set< T > &v ) {
	os << "[";
	for(auto it = v.begin(); it != v.end(); ++it) {
		if( it != v.begin()) os << ", ";
		os << *it;
	}
    return os << "]";
}

template < typename F, typename S >
ostream &operator << ( ostream & os, const map< F, S > &v ) {
	os << "[";
	for(auto it = v.begin(); it != v.end(); ++it) {
		if( it != v.begin() ) os << ", ";
		os << it -> first << " = " << it -> second ;
	}
    return os << "]";
}

#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)

clock_t tStart = clock();
#define timeStamp dbg("Execution Time: ", (double)(clock() - tStart)/CLOCKS_PER_SEC)

void faltu () { cerr << endl; }

template <typename T>
void faltu( T a[], int n ) {
	for(int i = 0; i < n; ++i) cerr << a[i] << ' ';
	cerr << endl;
}

template <typename T, typename ... hello>
void faltu( T arg, const hello &... rest) { cerr << arg << ' '; faltu(rest...); }

#else
#define dbg(args...)
#endif
/*
ll add(ll a, ll b) {
	ll ret = a+b;
	if(ret >= mod) ret -= mod;
	return ret;
}

ll subtract(ll a, ll b) {
	ll ret = a-b;
	if(ret < 0) ret += mod;
	return ret;
}

ll mult(ll a, ll b) {
	ll ret = a*b;
	if(ret >= mod) ret %= mod;
	return ret;
}

ll bigmod(ll a, ll b) {
	ll ret = 1;
	while(b) {
		if(b&1) ret = mult(ret, a);
		b >>= 1; a = mult(a, a);
	}
	return ret;
}
*/

//ll inverse(ll n) { return bigmod(n, mod-2); }

/*
#define PMX 1000000

int marked[PMX/64 + 2];
vi primes;

#define mark(x) marked[x >> 6] |= (1 << ((x & 63) >> 1))
#define check(x) (marked[x >> 6] & (1 << ((x & 63) >> 1)))

bool isPrime(int x)
{
	return (x > 1) && ((x == 2) || ((x & 1) && (!(check(x)))));
}

void seive(int n)
{
	int i, j;
	for(i = 3; i * i <= n; i += 2) if(!check(i)) for(j = i * i; j <= n; j += i << 1) mark(j);
	primes.PB(2);
	for(i = 3; i <= n; i += 2) if(isPrime(i)) primes.PB(i);
}
*/

//Direction Array
//int fx[]={1, -1, 0, 0}; int fy[]={0, 0, 1, -1};
//int fx[]={0, 0, 1, -1, -1, 1, -1, 1}; int fy[]={-1, 1, 0, 0, 1, 1, -1, -1};

//bit manipulation
/*
bool checkBit(int n, int i) { return (n&(1<<i)); }
int setBit(int n, int i) { return (n|(1<<i)); }
int resetBit(int n, int i) { return (n&(~(1<<i))); }
*/
//end of template

#define MX 100007

set< pair< pll, int > > s1, s2, c1, c2;
//pair< pll, int > v;
set< pii > st, c;
ll s[MX], vis[MX], a[MX], b[MX], SS;
int n;

ll f(ll tp1)
{
	int i;
	c = st;
	//c1 = s1;
	//c2 = s2;
	for(i = 0; i < n; i ++) vis[i] = s[i];

	ll ret = 0;
	ll tot = tp1 * SS;
	ll sum = 0;
	auto it = c.begin();
	while(sum < tot && it != c.end()) {
		pii v = *it;
		ll ad = min(tot - sum, vis[v.S]);
		sum += ad;
		ret += ad * a[v.S];
		it ++;
		vis[v.S] -= ad;
	}

	/*ll ret = 0;
	ll tot = tp1 * SS;
	ll sum = 0;
	auto it = c1.begin();
	while(sum < tot && it != c1.end()) {
		v = *it;
		ll ad = min(tot - sum, vis[v.S]);
		sum += ad;
		ret += ad * a[v.S];
		it ++;
		vis[v.S] -= ad;
	}
	tot = tp2 * SS;
	sum = 0;
	it = c2.begin();
	while(sum < tot && it != c2.end()) {
		v = *it;
		ll ad = min(tot - sum, vis[v.S]);
		sum += ad;
		ret += ad * b[v.S];
		it ++;
		vis[v.S] -= ad;
	}*/
	for(i = 0; i < n; i ++) ret += vis[i] * b[i];
	return ret;
}

int main () {
#ifdef kfoozminus
//	freopen("in", "r", stdin);
//	freopen("out", "w", stdout);
#endif
	int i;
	ll tot, mn, mx, mid1, mid2, ans1, ans2, sum = 0, ans = 0;

	scanf("%d %lld", &n, &SS);
	for(i = 0; i < n; i ++) {
		scanf("%lld %lld %lld", &s[i], &a[i], &b[i]);
		st.insert( {-(a[i] - b[i]), i} );
		//s2.insert( {{-(b[i] - a[i]), -b[i]}, i} );
		sum += s[i];
	}
	tot = (sum + SS - 1)/SS;

	mn = 0;
	mx = tot;
	//for(i = 0; i <= tot; i ++) ans = max(ans, f(i, tot - i));
	while(mn < mx) {
		mid1 = mn + (mx - mn) /3;
		mid2 = mx - (mx - mn) /3;
		dbg(mid1, mid2);

		ans1 = f(mid1);
		ans2 = f(mid2);
		dbg(ans1, ans2);

		if(ans1 < ans2) mn = mid1 + 1;
		else mx = mid2 - 1;
	}
	for(i = max(0LL, mn - 100); i <= min(tot, mn + 100); i ++) ans = max(ans, f(i));
	cout << ans << endl;

//	timeStamp;
	return 0;
}


/*
ID: kfoozminus
TASK:
LANG: C++
*/

#include <bits/stdc++.h>
using namespace std;
//typedefs
/*
typedef long long ll;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <int, pii> piii;
typedef vector <int> vi;
typedef vector <ll> vl;
typedef pair <ll, ll> pll;
const double PI = acos(-1);
*/
//typedefs
#define ll long long
#define ull unsigned long long
#define pii pair <int, int>
#define piii pair <int, pii>
#define vi vector <int>
#define vl vector <ll>
#define pll pair <ll, ll>
#define PI acos(-1)
//defines
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define mem(a, b) memset(a, b, sizeof(a))
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define SQR(a) ((a)*(a))
#define inf 100000000
#define mod 1000000007
#define mod1 1000000007
#define mod2 1000000009
#define b1 43
#define b2 41
#define EPS 1e-9
//define harmonic(n) 0.57721566490153286l+log(n)
#define nl puts("")
#define odd(n) ((n)&1)
#define even(n) (!((n)&1))
#define vsort(v) sort(v.begin(), v.end())
#define lc (node<<1)
#define rc ((node<<1)|1)
#define ran(a, b) ((((rand() << 15) ^ rand()) % ((b) - (a) + 1)) + (a))
//loop
#define rep(i, n) for(int i = 0; i < n; ++i)
#define REP(i, n) for(int i = 1; i <= n; ++i)
//input
#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d%d", &a, &b)
#define siii(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define sl(a) scanf("%lld", &a)
#define sll(a, b) scanf("%lld%lld", &a, &b)
#define slll(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)
#define sd(a) scanf("%lf", &a)
#define sc(a) scanf("%c", &a)
#define sst(a) scanf("%s", a)
inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
//debug
#ifdef kfoozminus

template < typename F, typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
	return os << "(" << p.first << ", " << p.second << ")";
}

template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
	os << "{";
	for(auto it = v.begin(); it != v.end(); ++it) {
		if( it != v.begin() ) os << ", ";
		os << *it;
	}
    return os << "}";
}

template < typename T >
ostream &operator << ( ostream & os, const set< T > &v ) {
	os << "[";
	for(auto it = v.begin(); it != v.end(); ++it) {
		if( it != v.begin()) os << ", ";
		os << *it;
	}
    return os << "]";
}

template < typename F, typename S >
ostream &operator << ( ostream & os, const map< F, S > &v ) {
	os << "[";
	for(auto it = v.begin(); it != v.end(); ++it) {
		if( it != v.begin() ) os << ", ";
		os << it -> first << " = " << it -> second ;
	}
    return os << "]";
}

#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)

clock_t tStart = clock();
#define timeStamp dbg("Execution Time: ", (double)(clock() - tStart)/CLOCKS_PER_SEC)

void faltu () { cerr << endl; }

template <typename T>
void faltu( T a[], int n ) {
	for(int i = 0; i < n; ++i) cerr << a[i] << ' ';
	cerr << endl;
}

template <typename T, typename ... hello>
void faltu( T arg, const hello &... rest) { cerr << arg << ' '; faltu(rest...); }

#else
#define dbg(args...)
#endif
/*
ll add(ll a, ll b) {
	ll ret = a+b;
	if(ret >= mod) ret -= mod;
	return ret;
}

ll subtract(ll a, ll b) {
	ll ret = a-b;
	if(ret < 0) ret += mod;
	return ret;
}

ll mult(ll a, ll b) {
	ll ret = a*b;
	if(ret >= mod) ret %= mod;
	return ret;
}

ll bigmod(ll a, ll b) {
	ll ret = 1;
	while(b) {
		if(b&1) ret = mult(ret, a);
		b >>= 1; a = mult(a, a);
	}
	return ret;
}
*/

//ll inverse(ll n) { return bigmod(n, mod-2); }

/*
#define PMX 1000000

int marked[PMX/64 + 2];
vi primes;

#define mark(x) marked[x >> 6] |= (1 << ((x & 63) >> 1))
#define check(x) (marked[x >> 6] & (1 << ((x & 63) >> 1)))

bool isPrime(int x)
{
	return (x > 1) && ((x == 2) || ((x & 1) && (!(check(x)))));
}

void seive(int n)
{
	int i, j;
	for(i = 3; i * i <= n; i += 2) if(!check(i)) for(j = i * i; j <= n; j += i << 1) mark(j);
	primes.PB(2);
	for(i = 3; i <= n; i += 2) if(isPrime(i)) primes.PB(i);
}
*/

//Direction Array
//int fx[]={1, -1, 0, 0}; int fy[]={0, 0, 1, -1};
//int fx[]={0, 0, 1, -1, -1, 1, -1, 1}; int fy[]={-1, 1, 0, 0, 1, 1, -1, -1};

//bit manipulation
/*
bool checkBit(int n, int i) { return (n&(1<<i)); }
int setBit(int n, int i) { return (n|(1<<i)); }
int resetBit(int n, int i) { return (n&(~(1<<i))); }
*/
//end of template

#define MX 100007

set< pair< pll, int > > s1, s2, c1, c2;
//pair< pll, int > v;
set< pii > st, c;
ll s[MX], vis[MX], a[MX], b[MX], SS;
int n;

ll f(ll tp1)
{
	int i;
	c = st;
	//c1 = s1;
	//c2 = s2;
	for(i = 0; i < n; i ++) vis[i] = s[i];

	ll ret = 0;
	ll tot = tp1 * SS;
	ll sum = 0;
	auto it = c.begin();
	while(sum < tot && it != c.end()) {
		pii v = *it;
		ll ad = min(tot - sum, vis[v.S]);
		sum += ad;
		ret += ad * a[v.S];
		it ++;
		vis[v.S] -= ad;
	}

	/*ll ret = 0;
	ll tot = tp1 * SS;
	ll sum = 0;
	auto it = c1.begin();
	while(sum < tot && it != c1.end()) {
		v = *it;
		ll ad = min(tot - sum, vis[v.S]);
		sum += ad;
		ret += ad * a[v.S];
		it ++;
		vis[v.S] -= ad;
	}
	tot = tp2 * SS;
	sum = 0;
	it = c2.begin();
	while(sum < tot && it != c2.end()) {
		v = *it;
		ll ad = min(tot - sum, vis[v.S]);
		sum += ad;
		ret += ad * b[v.S];
		it ++;
		vis[v.S] -= ad;
	}*/
	for(i = 0; i < n; i ++) ret += vis[i] * b[i];
	return ret;
}

int main () {
#ifdef kfoozminus
//	freopen("in", "r", stdin);
//	freopen("out", "w", stdout);
#endif
	int i;
	ll tot, mn, mx, mid1, mid2, ans1, ans2, sum = 0, ans = 0;

	scanf("%d %lld", &n, &SS);
	for(i = 0; i < n; i ++) {
		scanf("%lld %lld %lld", &s[i], &a[i], &b[i]);
		st.insert( {-(a[i] - b[i]), i} );
		//s2.insert( {{-(b[i] - a[i]), -b[i]}, i} );
		sum += s[i];
	}
	tot = (sum + SS - 1)/SS;

	mn = 0;
	mx = tot;
	//for(i = 0; i <= tot; i ++) ans = max(ans, f(i, tot - i));
	while(mn < mx) {
		mid1 = mn + (mx - mn) /3;
		mid2 = mx - (mx - mn) /3;
		dbg(mid1, mid2);

		ans1 = f(mid1);
		ans2 = f(mid2);
		dbg(ans1, ans2);

		if(ans1 < ans2) mn = mid1 + 1;
		else mx = mid2 - 1;
	}
	ans = f(mn);
	cout << ans << endl;

//	timeStamp;
	return 0;
}


/*
ID: kfoozminus
TASK:
LANG: C++
*/

#include <bits/stdc++.h>
using namespace std;
//typedefs
/*
typedef long long ll;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <int, pii> piii;
typedef vector <int> vi;
typedef vector <ll> vl;
typedef pair <ll, ll> pll;
const double PI = acos(-1);
*/
//typedefs
#define ll long long
#define ull unsigned long long
#define pii pair <int, int>
#define piii pair <int, pii>
#define vi vector <int>
#define vl vector <ll>
#define pll pair <ll, ll>
#define PI acos(-1)
//defines
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define mem(a, b) memset(a, b, sizeof(a))
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define SQR(a) ((a)*(a))
#define inf 100000000
#define mod 1000000007
#define mod1 1000000007
#define mod2 1000000009
#define b1 43
#define b2 41
#define EPS 1e-9
//define harmonic(n) 0.57721566490153286l+log(n)
#define nl puts("")
#define odd(n) ((n)&1)
#define even(n) (!((n)&1))
#define vsort(v) sort(v.begin(), v.end())
#define lc (node<<1)
#define rc ((node<<1)|1)
#define ran(a, b) ((((rand() << 15) ^ rand()) % ((b) - (a) + 1)) + (a))
//loop
#define rep(i, n) for(int i = 0; i < n; ++i)
#define REP(i, n) for(int i = 1; i <= n; ++i)
//input
#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d%d", &a, &b)
#define siii(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define sl(a) scanf("%lld", &a)
#define sll(a, b) scanf("%lld%lld", &a, &b)
#define slll(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)
#define sd(a) scanf("%lf", &a)
#define sc(a) scanf("%c", &a)
#define sst(a) scanf("%s", a)
inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
//debug
#ifdef kfoozminus

template < typename F, typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
	return os << "(" << p.first << ", " << p.second << ")";
}

template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
	os << "{";
	for(auto it = v.begin(); it != v.end(); ++it) {
		if( it != v.begin() ) os << ", ";
		os << *it;
	}
    return os << "}";
}

template < typename T >
ostream &operator << ( ostream & os, const set< T > &v ) {
	os << "[";
	for(auto it = v.begin(); it != v.end(); ++it) {
		if( it != v.begin()) os << ", ";
		os << *it;
	}
    return os << "]";
}

template < typename F, typename S >
ostream &operator << ( ostream & os, const map< F, S > &v ) {
	os << "[";
	for(auto it = v.begin(); it != v.end(); ++it) {
		if( it != v.begin() ) os << ", ";
		os << it -> first << " = " << it -> second ;
	}
    return os << "]";
}

#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)

clock_t tStart = clock();
#define timeStamp dbg("Execution Time: ", (double)(clock() - tStart)/CLOCKS_PER_SEC)

void faltu () { cerr << endl; }

template <typename T>
void faltu( T a[], int n ) {
	for(int i = 0; i < n; ++i) cerr << a[i] << ' ';
	cerr << endl;
}

template <typename T, typename ... hello>
void faltu( T arg, const hello &... rest) { cerr << arg << ' '; faltu(rest...); }

#else
#define dbg(args...)
#endif
/*
ll add(ll a, ll b) {
	ll ret = a+b;
	if(ret >= mod) ret -= mod;
	return ret;
}

ll subtract(ll a, ll b) {
	ll ret = a-b;
	if(ret < 0) ret += mod;
	return ret;
}

ll mult(ll a, ll b) {
	ll ret = a*b;
	if(ret >= mod) ret %= mod;
	return ret;
}

ll bigmod(ll a, ll b) {
	ll ret = 1;
	while(b) {
		if(b&1) ret = mult(ret, a);
		b >>= 1; a = mult(a, a);
	}
	return ret;
}
*/

//ll inverse(ll n) { return bigmod(n, mod-2); }

/*
#define PMX 1000000

int marked[PMX/64 + 2];
vi primes;

#define mark(x) marked[x >> 6] |= (1 << ((x & 63) >> 1))
#define check(x) (marked[x >> 6] & (1 << ((x & 63) >> 1)))

bool isPrime(int x)
{
	return (x > 1) && ((x == 2) || ((x & 1) && (!(check(x)))));
}

void seive(int n)
{
	int i, j;
	for(i = 3; i * i <= n; i += 2) if(!check(i)) for(j = i * i; j <= n; j += i << 1) mark(j);
	primes.PB(2);
	for(i = 3; i <= n; i += 2) if(isPrime(i)) primes.PB(i);
}
*/

//Direction Array
//int fx[]={1, -1, 0, 0}; int fy[]={0, 0, 1, -1};
//int fx[]={0, 0, 1, -1, -1, 1, -1, 1}; int fy[]={-1, 1, 0, 0, 1, 1, -1, -1};

//bit manipulation
/*
bool checkBit(int n, int i) { return (n&(1<<i)); }
int setBit(int n, int i) { return (n|(1<<i)); }
int resetBit(int n, int i) { return (n&(~(1<<i))); }
*/
//end of template

#define MX 100007

set< pair< pll, int > > s1, s2, c1, c2;
//pair< pll, int > v;
set< pair< pii, pii > > st, c;
ll s[MX], vis[MX], a[MX], b[MX], SS;
int n;

ll f(ll tp1, ll tp2)
{
	int i;
	c = st;
	//c1 = s1;
	//c2 = s2;
	for(i = 0; i < n; i ++) vis[i] = s[i];

	ll ret = 0;
	ll tot = tp1 * SS;
	ll sum = 0;
	auto it = c.begin();
	while(sum < tot && it != c.end()) {
		pair< pii, pii> v = *it;
		ll ad = min(tot - sum, vis[v.S.S]);
		sum += ad;
		ret += ad * a[v.S.S];
		it ++;
		vis[v.S.S] -= ad;
	}

	/*ll ret = 0;
	ll tot = tp1 * SS;
	ll sum = 0;
	auto it = c1.begin();
	while(sum < tot && it != c1.end()) {
		v = *it;
		ll ad = min(tot - sum, vis[v.S]);
		sum += ad;
		ret += ad * a[v.S];
		it ++;
		vis[v.S] -= ad;
	}
	tot = tp2 * SS;
	sum = 0;
	it = c2.begin();
	while(sum < tot && it != c2.end()) {
		v = *it;
		ll ad = min(tot - sum, vis[v.S]);
		sum += ad;
		ret += ad * b[v.S];
		it ++;
		vis[v.S] -= ad;
	}*/
	for(i = 0; i < n; i ++) ret += vis[i] * b[i];
	return ret;
}

int main () {
#ifdef kfoozminus
//	freopen("in", "r", stdin);
//	freopen("out", "w", stdout);
#endif
	int i;
	ll tot, mn, mx, mid1, mid2, ans1, ans2, sum = 0, ans = 0;

	scanf("%d %lld", &n, &SS);
	for(i = 0; i < n; i ++) {
		scanf("%lld %lld %lld", &s[i], &a[i], &b[i]);
		st.insert( {{-(a[i] - b[i]), -a[i]}, {b[i], i}} );
		//s2.insert( {{-(b[i] - a[i]), -b[i]}, i} );
		sum += s[i];
	}
	tot = (sum + SS - 1)/SS;

	mn = 0;
	mx = tot;
	for(i = 0; i <= tot; i ++) ans = max(ans, f(i, tot - i));
	/*while(mn < mx) {
		mid1 = mn + (mx - mn) /3;
		mid2 = mx - (mx - mn) /3;
		dbg(mid1, mid2);

		ans1 = f(mid1, tot - mid1);
		ans2 = f(mid2, tot - mid2);
		dbg(ans1, ans2);

		if(ans1 < ans2) mn = mid1 + 1;
		else mx = mid2 - 1;
	}
	ans = f(mn, tot - mn);*/
	cout << ans << endl;

//	timeStamp;
	return 0;
}