#include <bits/stdc++.h>
using namespace std;
struct type {
  long long slices;
  long long diff;
  long long a;
  long long b;
};
type type1[100001];
type type2[100001];
bool sort_by_diff(const type& lhs, const type& rhs) {
  return lhs.diff > rhs.diff;
}
int main() {
  int s, n;
  scanf("%d%d", &n, &s);
  long long i = 0, count1 = 0, count2 = 0;
  long long slice, a, b, num_a = 0, num_b = 0;
  for (i = 0; i < n; i++) {
    scanf("%lld%lld%lld", &slice, &a, &b);
    if (a > b) {
      type1[count1].diff = a - b;
      type1[count1].slices = slice;
      num_a += slice;
      type1[count1].a = a;
      type1[count1++].b = b;
    } else {
      type2[count2].diff = b - a;
      type2[count2].slices = slice;
      num_b += slice;
      type2[count2].a = a;
      type2[count2++].b = b;
    }
  }
  std::sort(type1, type1 + count1, sort_by_diff);
  std::sort(type2, type2 + count2, sort_by_diff);
  long long max = 0;
  long long j, k;
  long long num_a_slices, num_b_slices;
  num_a_slices = (num_a / s) * s;
  long long rem_a = num_a % s;
  long long rem_b = num_b % s;
  num_b_slices = (num_b / s) * s;
  for (j = 0; j < count1; j++) {
    if (num_a_slices >= type1[j].slices) {
      max += (type1[j].slices) * (type1[j].a);
      num_a_slices = num_a_slices - type1[j].slices;
      continue;
    } else {
      max += (num_a_slices) * (type1[j].a);
      type1[j].slices = type1[j].slices - num_a_slices;
      break;
    }
  }
  for (k = 0; k < count2; k++) {
    if (num_b_slices >= type2[k].slices) {
      max += (type2[k].slices) * (type2[k].b);
      num_b_slices = num_b_slices - type2[k].slices;
      continue;
    } else {
      max += (num_b_slices) * (type2[k].b);
      type2[k].slices = type2[k].slices - num_b_slices;
      break;
    }
  }
  if ((rem_a + rem_b) > s) {
    for (j = j; j < count1; j++) {
      max += (type1[j].slices) * (type1[j].a);
    }
    for (k = k; k < count2; k++) {
      max += (type2[k].slices) * (type2[k].b);
    }
  } else {
    long long var1 = 0, var2 = 0;
    long long j_ind = j, k_ind = k;
    for (j = j_ind; j < count1; j++) {
      var1 += (type1[j].slices) * (type1[j].a);
    }
    for (k = k_ind; k < count2; k++) {
      var1 += (type2[k].slices) * (type2[k].a);
    }
    for (j = j_ind; j < count1; j++) {
      var2 += (type1[j].slices) * (type1[j].b);
    }
    for (k = k_ind; k < count2; k++) {
      var2 += (type2[k].slices) * (type2[k].b);
    }
    if (var1 > var2)
      max += var1;
    else
      max += var2;
  }
  printf("%lld\n", max);
  return 0;
}
