#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
vector<pair<long long, long long> > aa, bb;
long long S, s, a, b;
long long na, nb, ta, tb, ans;
int main() {
  int n;
  while (~scanf("%d%lld", &n, &S)) {
    ans = 0;
    na = 0;
    nb = 0;
    aa.clear();
    bb.clear();
    pair<long long, long long> k;
    for (int i = 0; i < n; i++) {
      scanf("%lld%lld%lld", &s, &a, &b);
      if (a > b) {
        ans += s * a;
        na += s;
        k.first = a - b;
        k.second = s;
        aa.push_back(k);
      } else {
        ans += s * b;
        nb += s;
        k.first = b - a;
        k.second = s;
        bb.push_back(k);
      }
    }
    na %= S;
    nb %= S;
    if (na + nb > S) {
      printf("%I64d\n", ans);
      continue;
    }
    sort(aa.begin(), aa.end());
    sort(bb.begin(), bb.end());
    ta = 0;
    tb = 0;
    for (int i = 0; na; i++) {
      ta += min(na, aa[i].second) * aa[i].first;
      na -= min(na, aa[i].second);
    }
    for (int i = 0; nb; i++) {
      tb += min(nb, bb[i].second) * bb[i].first;
      nb -= min(nb, bb[i].second);
    }
    ans -= min(ta, tb);
    printf("%I64d\n", ans);
  }
  return 0;
}
