#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, long long> > a, b;
long long n, s, ans, na, nb, ta, tb, i, j;
long long s1, a1, b1;
pair<long long, long long> k;
int main() {
  cin >> n >> s;
  for (i = 0; i < n; i++) {
    cin >> s1 >> a1 >> b1;
    if (a1 > b1) {
      ans += s1 * a1;
      na += s1;
      k.first = a1 - b1;
      k.second = s1;
      a.push_back(k);
    } else {
      ans += s1 * b1;
      nb += s1;
      k.first = b1 - a1;
      k.second = s1;
      b.push_back(k);
    }
  }
  na %= s;
  nb %= s;
  if (na + nb > s) {
    cout << ans;
    return 0;
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  for (i = 0; na; i++) {
    ta += min(na, a[i].second) * a[i].first;
    na -= min(na, a[i].second);
  }
  for (i = 0; nb; i++) {
    tb += min(nb, b[i].second) * b[i].first;
    nb -= min(nb, b[i].second);
  }
  ans -= min(ta, tb);
  cout << ans;
}
