#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 10;
struct Node {
  int a, b, c, d;
  Node() {}
  Node(int a, int b, int c) : a(a), b(b), c(c) { d = abs(a - b); }
  inline bool operator<(const Node& tmp) const { return d < tmp.d; }
} a[maxn], L[maxn], R[maxn];
int n, S, m;
inline void init() {
  long long sum = 0;
  scanf("%d%d", &n, &S);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d%d", &a[i].c, &a[i].a, &a[i].b);
    a[i].d = abs(a[i].a - a[i].b);
    sum += a[i].c;
  }
  m = (sum + S - 1) / S;
}
inline void exec() {
  int szl = 0, szr = 0;
  long long suml = 0, sumr = 0;
  long long ans = 0;
  for (int i = 1; i <= n; i++)
    if (a[i].a >= a[i].b) {
      suml += a[i].c;
      L[++szl] = a[i];
      ans += (long long)a[i].c * a[i].a;
    } else {
      sumr += a[i].c;
      R[++szr] = a[i];
      ans += (long long)a[i].c * a[i].b;
    }
  sort(L + 1, L + szl + 1);
  sort(R + 1, R + szr + 1);
  int cnt = (suml + S - 1) / S + (sumr + S - 1) / S;
  if (cnt <= m) return (void)printf("%I64d\n", ans);
  long long res1 = 0, res2 = 0;
  int rem = suml % S;
  int pos = 1;
  while (rem) {
    cnt = min(rem, L[pos].c);
    res1 += (long long)L[pos].d * cnt;
    rem -= cnt;
    pos++;
  }
  rem = sumr % S;
  pos = 1;
  while (rem) {
    cnt = min(rem, R[pos].c);
    res2 += (long long)R[pos].d * cnt;
    rem -= cnt;
    pos++;
  }
  printf("%I64d\n", ans - min(res1, res2));
}
int main() {
  if (fopen("input.in", "r") != NULL) {
    freopen("input.in", "r", stdin);
    freopen("output.out", "w", stdout);
  }
  init();
  exec();
  return 0;
}
