#include <bits/stdc++.h>
using namespace std;
long long x[100005], a[100005], b[100005];
vector<pair<int, int> > v1, v2;
int main() {
  std::ios::sync_with_stdio(false);
  int n, s, i, val1, c = 0;
  long long val = 0;
  long long x1 = 0, x2 = 0, y1 = 0, y2 = 0, y3 = 0;
  long long ans = -100;
  cin >> n >> s;
  for (i = 0; i < n; i++) {
    cin >> x[i] >> a[i] >> b[i];
    val += x[i];
  }
  if (val % s == 0) {
    val = val / s;
  } else {
    val = val / s + 1;
  }
  x1 = 0;
  x2 = 0;
  for (i = 0; i < n; i++) {
    if (a[i] > b[i]) {
      x1 += x[i];
      y1 += x[i] * a[i];
      v1.push_back(make_pair(b[i] - a[i], i));
    } else {
      x2 += x[i];
      y2 += x[i] * b[i];
      v2.push_back(make_pair(a[i] - b[i], i));
    }
  }
  sort(v1.rbegin(), v1.rend());
  sort(v2.rbegin(), v2.rend());
  if (x1 % s == 0 || x2 % s == 0) {
    cout << y1 + y2 << endl;
  } else {
    if (x1 % s + x2 % s > s) {
      cout << y1 + y2 << endl;
    } else {
      val1 = 0;
      y3 = y1;
      c = 0;
      int fl = 0;
      int c1 = x[v1[val1].second];
      while (1) {
        while (c1--) {
          c++;
          y3 += v1[val1].first;
          if (c == x1 % s) {
            fl = 1;
            break;
          }
        }
        if (fl == 1) {
          break;
        }
        val1++;
        c1 = x[v1[val1].second];
      }
      fl = 0;
      ans = y3 + y2;
      val1 = 0;
      c = 0;
      c1 = x[v2[val1].second];
      while (1) {
        while (c1--) {
          c++;
          y2 += v2[val1].first;
          if (c == x2 % s) {
            fl = 1;
            break;
          }
        }
        if (fl == 1) break;
        val1++;
        c1 = x[v2[val1].second];
      }
      ans = max(ans, y1 + y2);
      cout << ans << endl;
    }
  }
}
