#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, s;
  cin >> n >> s;
  vector<pair<long long, long long> > pa;
  vector<pair<long long, long long> > pb;
  long long numa, numb, numn;
  long long happa, happb, happn;
  numa = numb = numn = 0;
  happa = happb = happn = 0;
  for (long long i = 0; i < n; i++) {
    long long n1, a1, b1;
    cin >> n1 >> a1 >> b1;
    if (a1 > b1) {
      pa.push_back(make_pair(a1 - b1, n1));
      numa += n1;
      happa += a1 * n1;
    } else if (b1 > a1) {
      pb.push_back(make_pair(b1 - a1, n1));
      numb += n1;
      happb += b1 * n1;
    } else {
      numn += n1;
      happn += n1 * a1;
    }
  }
  long long lefta = numa % s;
  long long leftb = numb % s;
  long long leftn = numn;
  if (lefta + leftb + leftn > s) {
    long long ans = happa + happb + happn;
    cout << ans << endl;
  } else {
    sort(pa.begin(), pa.end());
    long long i = 0;
    long long lossa = 0;
    while (i < pa.size() && lefta > 0) {
      long long use = min(pa[i].second, lefta);
      lefta -= use;
      lossa += use * pa[i].first;
      i++;
    }
    sort(pb.begin(), pb.end());
    i = 0;
    long long lossb = 0;
    while (i < pb.size() && leftb > 0) {
      long long use = min(pb[i].second, leftb);
      leftb -= use;
      lossb += use * pb[i].first;
      i++;
    }
    long long ans = -1 * min(lossa, lossb);
    ans += happa + happb + happn;
    cout << ans << endl;
  }
  return 0;
}
