#include <bits/stdc++.h>
struct ST {
  int h, h1, h2, nom, type;
};
bool operator<(ST x, ST y) { return x.h1 - x.h2 > y.h1 - y.h2; }
ST st[200000];
int k[100000], cnt[100000];
long long a, b, c, d, fc, fd, pica, picar, pica1, pica2, n, n2, ans, s, s1, s2,
    t, i;
long long sum(long long pica1) {
  ans = 0;
  pica2 = pica - pica1;
  pica1 = pica1 * picar;
  pica2 = pica2 * picar;
  for (i = 0; i < n; i++) cnt[i] = k[i];
  for (i = 0; i < n2; i++) {
    if (cnt[st[i].nom] == 0) continue;
    if (st[i].type == 1) {
      if (pica1 == 0) continue;
      t = cnt[st[i].nom];
      if (t > pica1) t = pica1;
      ans = ans + t * st[i].h;
      pica1 -= t;
      cnt[st[i].nom] -= t;
    } else {
      if (pica2 == 0) continue;
      t = cnt[st[i].nom];
      if (t > pica2) t = pica2;
      ans = ans + t * st[i].h;
      pica2 -= t;
      cnt[st[i].nom] -= t;
    }
  }
  return ans;
}
using namespace std;
int main() {
  scanf("%d %d", &n, &picar);
  for (i = 0; i < n; i++) {
    scanf("%d %d %d", &k[i], &s1, &s2);
    st[n2].h = s1;
    st[n2].h1 = s1;
    st[n2].h2 = s2;
    st[n2].nom = i;
    st[n2++].type = 1;
    st[n2].h = s2;
    st[n2].h1 = s2;
    st[n2].h2 = s1;
    st[n2].nom = i;
    st[n2++].type = 2;
    s = s + k[i];
  }
  pica = s / picar;
  if (pica * picar < s) pica++;
  sort(st, st + n2);
  a = 0;
  b = pica;
  while (a < b) {
    c = a + (b - a) / 3;
    d = b - (b - a) / 3;
    fc = sum(c);
    fd = sum(d);
    if (fc == fd) {
      a = c + 1;
      b = d;
    }
    if (fc < fd) a = c + 1;
    if (fc > fd) b = d - 1;
  }
  cout << sum(a);
  return 0;
}
