#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const int INF = 0x3f3f3f3f;
struct node {
  long long x, a, b;
  long long c;
} p[maxn];
int n, m;
bool cmp(node _p, node _q) { return _p.c < _q.c; }
int main() {
  scanf("%d %d", &n, &m);
  long long ha1 = 0, ha2 = 0, num1 = 0, num2 = 0;
  for (int i = 0; i < n; i++) {
    scanf("%lld %lld %lld", &p[i].x, &p[i].a, &p[i].b);
    p[i].c = (p[i].a - p[i].b);
    if (p[i].a >= p[i].b) {
      ha1 += p[i].x * p[i].a;
      num1 += p[i].x;
    } else {
      ha2 += p[i].x * p[i].b;
      num2 += p[i].x;
    }
  }
  if (num1 % m == 0 && num2 % m == 0) {
    printf("%lld\n", ha1 + ha2);
    return 0;
  }
  sort(p, p + n, cmp);
  long long res1 = num1 % m, cha1 = 0, res2 = num2 % m, cha2 = 0;
  if (res1 + res2 > m) {
    res1 = 0;
    res2 = 0;
  }
  for (int i = 0; i < n; i++) {
    if (p[i].c >= 0) {
      if (res1 >= p[i].x) {
        cha1 += p[i].c * p[i].x;
        res1 -= p[i].x;
      } else {
        cha1 += res1 * p[i].c;
        res1 = 0;
        break;
      }
    }
  }
  for (int i = n - 1; i >= 0; i--) {
    if (p[i].c < 0) {
      if (res2 >= p[i].x) {
        cha2 += (-p[i].c) * p[i].x;
        res2 -= p[i].x;
      } else {
        cha2 += (-p[i].c) * res2;
        res2 = 0;
        break;
      }
    }
  }
  long long ans = max(ha1 + ha2 - cha1, ha1 + ha2 - cha2);
  printf("%lld\n", ans);
  return 0;
}
