#include <bits/stdc++.h>
using namespace std;
long long p[4 * 100010], n, s, cnt1, cnt2, t1, t2, mx, mx1, mx2;
vector<pair<int, int> > p1, p2;
int main() {
  cin >> n >> s;
  for (int i = 1; i <= n; i++) {
    cin >> p[i] >> t1 >> t2;
    if (t1 >= t2) {
      cnt1 += p[i];
      mx = mx + p[i] * 1LL * t1;
      p1.push_back(make_pair(t1 - t2, i));
    } else {
      cnt2 += p[i];
      mx = mx + p[i] * 1LL * t2;
      p2.push_back(make_pair(t2 - t1, i));
    }
  }
  sort(p1.begin(), p1.end());
  sort(p2.begin(), p2.end());
  if (cnt1 % s + cnt2 % s > s) {
    cout << mx << endl;
    return 0;
  }
  mx1 = mx;
  long long kol = cnt1 % s;
  long long idx = 0;
  for (int i = 0; i < kol; i++) {
    if (p[p1[idx].second] > 0) {
      p[p1[idx].second]--;
      mx1 -= p1[idx].first;
    } else {
      idx++;
      i--;
    }
  }
  mx2 = mx;
  kol = cnt2 % s;
  idx = 0;
  for (int i = 0; i < kol; i++) {
    if (p[p2[idx].second] > 0) {
      p[p2[idx].second]--;
      mx2 -= p2[idx].first;
    } else {
      idx++;
      i--;
    }
  }
  cout << max(mx1, mx2) << endl;
  return 0;
}
