#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
long long mx, equality, x;
vector<long long> eq;
long long fun(long long t1, long long t2, bool flag,
              vector<pair<long long, long long> > diff1,
              vector<pair<long long, long long> > diff2) {
  for (int i = 0; i < (int)eq.size() && flag; i++) {
    diff1.push_back(make_pair(0, eq[i]));
  }
  for (int i = 0; i < (int)eq.size() && !flag; i++) {
    diff2.push_back(make_pair(0, eq[i]));
  }
  sort(diff1.begin(), diff1.end());
  sort(diff2.begin(), diff2.end());
  long long sum = equality;
  long long calc = ceil((double)(t1 + sum * flag) / x) +
                   ceil((double)(t2 + sum * !flag) / x);
  t1 = (t1 + sum * flag) % x, t2 = (t2 + sum * !flag) % x;
  long long ans1 = 0;
  for (int i = 0; i < (int)diff1.size() && calc > mx && t1; i++) {
    ans1 += min(diff1[i].second, t1) * diff1[i].first;
    t1 -= min(diff1[i].second, t1);
  }
  long long ans2 = 0;
  for (int i = 0; i < (int)diff2.size() && calc > mx && t2; i++) {
    ans2 += min(diff2[i].second, t2) * diff2[i].first;
    t2 -= min(diff2[i].second, t2);
  }
  return min(ans1, ans2);
}
int main() {
  int n;
  cin >> n >> x;
  vector<pair<long long, long long> > diff1, diff2;
  long long sum = 0, t1 = 0, t2 = 0;
  for (int i = 0; i < n; i++) {
    long long s, a, b;
    cin >> s >> a >> b;
    sum += (a > b ? a : b) * s;
    if (a == b) {
      eq.push_back(s);
      equality += s;
    } else if (a > b) {
      t1 += s;
      diff1.push_back(make_pair(a - b, s));
    } else {
      t2 += s;
      diff2.push_back(make_pair(b - a, s));
    }
  }
  mx = ceil((double)(t1 + t2) / x);
  long long ans1 = fun(t1, t2, 1, diff1, diff2);
  long long ans2 = fun(t1, t2, 0, diff1, diff2);
  cout << sum - min(ans1, ans2);
}
