#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
struct E {
  int num, a, b;
  bool operator<(const E t) const { return abs(a - b) < abs(t.a - t.b); }
} a[N];
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  long long sum = 0, ac = 0, bc = 0, Max = 0;
  for (int i = 1; i <= n; i++) {
    int as, bs, c;
    scanf("%d %d %d", &as, &bs, &c);
    a[i] = {as, bs, c};
    sum += as;
    if (bs >= c)
      ac += as, Max += (long long)as * bs;
    else
      bc += as, Max += (long long)as * c;
  }
  sort(a + 1, a + n + 1);
  long long cnt = (sum + m - 1) / m;
  long long a_cnt = ac % m, b_cnt = bc % m;
  if ((ac + m - 1) / m + (bc + m - 1) / m == cnt) {
    printf("%lld", Max);
    return 0;
  }
  long long Maxtemp = Max;
  for (int i = 1; i <= n; i++) {
    if (a[i].a < a[i].b) continue;
    long long t = min(a_cnt, (long long)a[i].num);
    Maxtemp -= t * (a[i].a - a[i].b);
    a_cnt -= t;
    if (!a_cnt) break;
  }
  long long Maxt = Max;
  for (int i = 1; i <= n; i++) {
    if (a[i].a >= a[i].b) continue;
    long long t = min(b_cnt, (long long)a[i].num);
    Maxt -= t * (a[i].b - a[i].a);
    b_cnt -= t;
    if (!b_cnt) break;
  }
  printf("%lld\n", max(Maxtemp, Maxt));
  return 0;
}
