#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, long long> > a, b;
long long n, s, ans, na, nb, ta, tb;
signed main() {
  long long ss, aa, bb;
  pair<long long, long long> k;
  scanf("%I64d%I64d", &n, &s);
  for (int i = 1; i <= n; i++) {
    scanf("%I64d%I64d%I64d", &ss, &aa, &bb);
    if (aa > bb) {
      ans += ss * aa;
      na += ss;
      k.first = aa - bb;
      k.second = ss;
      a.push_back(k);
    } else {
      ans += ss * bb;
      nb += ss;
      k.first = bb - aa;
      k.second = ss;
      b.push_back(k);
    }
  }
  na %= s;
  nb %= s;
  if (na + nb > s) {
    printf("%I64d\n", ans);
    return 0;
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  for (int i = 0; na; i++) {
    ta += min(na, a[i].second) * a[i].first;
    na -= min(na, a[i].second);
  }
  for (int i = 0; nb; i++) {
    tb += min(nb, b[i].second) * b[i].first;
    nb -= min(nb, b[i].second);
  }
  ans -= min(ta, tb);
  printf("%I64d\n", ans);
  return 0;
}
