#include <bits/stdc++.h>
using namespace std;
const int seed = 131;
const int maxn = 1e5 + 5;
const int mod = 998244353;
int n, s;
struct node {
  long long num, cha;
  bool operator<(const node &a) const { return cha < a.cha; }
};
vector<node> a, b;
int main() {
  scanf("%d%d", &n, &s);
  long long numa = 0, numb = 0;
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    long long s, x, y;
    scanf("%I64d%I64d%I64d", &s, &x, &y);
    if (x >= y) {
      node p;
      p.num = s;
      p.cha = x - y;
      numa += s;
      ans += s * x;
      a.push_back(p);
    } else {
      node p;
      p.cha = y - x;
      p.num = s;
      numb += s;
      ans += s * y;
      b.push_back(p);
    }
  }
  numa %= s;
  numb %= s;
  if (numa + numb > s)
    printf("%I64d\n", ans);
  else {
    long long sum1 = 0;
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    for (int i = 0; i < a.size(); i++) {
      if (numa == 0) break;
      if (a[i].num <= numa) {
        numa -= a[i].num;
        sum1 += a[i].num * a[i].cha;
      } else {
        sum1 += numa * a[i].cha;
        numa = 0;
      }
    }
    long long sum2 = 0;
    for (int i = 0; i < b.size(); i++) {
      if (numb == 0) break;
      if (b[i].num <= numb) {
        numb -= b[i].num;
        sum2 += b[i].num * b[i].cha;
      } else {
        sum2 += numb * b[i].cha;
        numb = 0;
      }
    }
    printf("%I64d\n", ans - min(sum1, sum2));
  }
  return 0;
}
