#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > x;
vector<pair<int, int> > y;
int main() {
  int n, m[100006][3], i, p, q;
  long long a = 0, b = 0, s = 0, u = 0, v = 0, ans = 0, t, t1, t2;
  cin >> n >> s;
  for (i = 0; i < n; i++) {
    cin >> m[i][0] >> m[i][1] >> m[i][2];
    if (m[i][2] > m[i][1]) {
      x.push_back(make_pair(m[i][2] - m[i][1], i));
      a += m[i][0];
    } else {
      y.push_back(make_pair(m[i][1] - m[i][2], i));
      b += m[i][0];
    }
  }
  sort(x.begin(), x.end());
  sort(y.begin(), y.end());
  t = a + b;
  t = ceil((double)t / s);
  t1 = ceil((double)a / s);
  t2 = ceil((double)b / s);
  a = a % s;
  b = b % s;
  if (t1 + t2 > t) {
    i = 0;
    while (a > 0) {
      p = x[i].first;
      q = x[i].second;
      if (a >= m[q][0])
        u += (long long)p * m[q][0];
      else
        u += (long long)p * a;
      a -= m[q][0];
      i++;
    }
    i = 0;
    while (b > 0) {
      p = y[i].first;
      q = y[i].second;
      if (b >= m[q][0])
        v += (long long)p * m[q][0];
      else
        v += (long long)p * b;
      b -= m[q][0];
      i++;
    }
  }
  for (i = 0; i < x.size(); i++) {
    q = x[i].second;
    ans += (long long)m[q][2] * m[q][0];
  }
  for (i = 0; i < y.size(); i++) {
    q = y[i].second;
    ans += (long long)m[q][1] * m[q][0];
  }
  ans -= min(u, v);
  cout << ans;
  return 0;
}
