#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
struct node {
  long long num, val;
} x[MAXN], y[MAXN];
long long n, s, ans, sum;
long long tot1, tot2, cnt1, cnt2;
long long less1, less2;
bool cmp(node a, node b) { return a.val < b.val; }
int main() {
  scanf("%lld%lld", &n, &s);
  for (register long long i = 1, c, a, b; i <= n; i++) {
    scanf("%lld%lld%lld", &c, &a, &b);
    if (a > b) {
      x[++tot1].val = a - b;
      x[tot1].num = c;
      cnt1 += c;
      ans += c * a;
    } else {
      y[++tot2].val = b - a;
      y[tot2].num = c;
      cnt2 += c;
      ans += c * b;
    }
  }
  cnt1 %= s, cnt2 %= s;
  if (cnt1 + cnt2 > s) {
    printf("%lld\n", ans);
    return 0;
  }
  sort(x + 1, x + tot1 + 1, cmp);
  sort(y + 1, y + tot2 + 1, cmp);
  for (int i = 1; i <= tot1; i++) {
    if (cnt1 >= x[i].num)
      less1 += x[i].num * x[i].val, cnt1 -= x[i].num;
    else {
      less1 += cnt1 * x[i].val;
      break;
    }
  }
  for (int i = 1; i <= tot2; i++) {
    if (cnt2 >= y[i].num)
      less2 += y[i].num * y[i].val, cnt2 -= y[i].num;
    else {
      less2 += cnt2 * y[i].val;
      break;
    }
  }
  printf("%lld\n", ans - min(less1, less2));
  return 0;
}
