#include <bits/stdc++.h>
using namespace std;
const int Mod = 1e9 + 7;
const double eps = 1e-8;
const int N = 1e5 + 10;
vector<pair<long long, long long> > a, b;
vector<pair<long long, long long> >::iterator it;
int main() {
  long long n, S, s, x, y;
  cin >> n >> S;
  long long ea = 0, eb = 0;
  long long hap = 0;
  while (n--) {
    cin >> s >> x >> y;
    if (x > y) {
      hap += s * x;
      ea += s;
      a.push_back(make_pair(x - y, s));
    } else {
      hap += s * y;
      eb += s;
      b.push_back(make_pair(y - x, s));
    }
  }
  ea %= S, eb %= S;
  if (ea + eb > S)
    cout << hap << endl;
  else {
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    long long da = 0, db = 0;
    for (it = a.begin(); it != a.end(); it++) {
      da += min((*it).second, ea) * (*it).first;
      ea -= min((*it).second, ea);
    }
    for (it = b.begin(); it != b.end(); it++) {
      db += min((*it).second, eb) * (*it).first;
      eb -= min((*it).second, eb);
    }
    hap -= min(da, db);
    cout << hap << endl;
  }
  return 0;
}
