#include <bits/stdc++.h>
using namespace std;
struct cunchu {
  long long x, y;
} temp;
bool cmp(struct cunchu q, struct cunchu w) { return q.x < w.x; }
struct cunchu v1[100010], v2[100010];
long long xia1 = 0, xia2 = 0;
int main() {
  int n, s;
  scanf("%d %d", &n, &s);
  long long sum = 0, he1 = 0, he2 = 0, a, b, c;
  for (int i = 0; i < n; i++) {
    scanf("%lld %lld %lld", &a, &b, &c);
    if (b > c) {
      he1 += a;
      sum += b * a;
      v1[xia1].x = b - c;
      v1[xia1++].y = a;
    } else {
      he2 += a;
      sum += c * a;
      v2[xia2].x = c - b;
      v2[xia2++].y = a;
    }
  }
  he1 = he1 % s;
  he2 = he2 % s;
  if ((he1 + he2) > s)
    printf("%lld", sum);
  else {
    sort(v1, v1 + xia1, cmp);
    sort(v2, v2 + xia2, cmp);
    long long ans1 = 0, ans2 = 0;
    int index = 0;
    while (he1 > 0) {
      ans1 += min(he1, v1[index].y) * v1[index].x;
      he1 -= v1[index++].y;
    }
    index = 0;
    while (he2 > 0) {
      ans2 += min(he2, v2[index].y) * v2[index].x;
      he2 -= v2[index++].y;
    }
    sum -= min(ans1, ans2);
    printf("%lld", sum);
  }
  return 0;
}
