#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int maxn = 1e6 + 10;
long long n, m, a, b, num;
struct node {
  long long a, num;
  node() {}
  node(long long a, long long num) : a(a), num(num) {}
} p1[maxn], p2[maxn];
int cmp(node a, node b) {
  if (a.a == b.a) return a.num < b.num;
  return a.a < b.a;
}
int main() {
  scanf("%lld%lld", &n, &m);
  int num1 = 0, num2 = 0;
  long long sum = 0, temp1 = 0, temp2 = 0, ret1 = 0, ret2 = 0;
  for (int i = 0; i < n; i++) {
    scanf("%lld%lld%lld", &num, &a, &b);
    if (a < b) {
      ret2 = (ret2 + num) % m;
      p2[num2++] = node(b - a, num);
      sum += b * num;
    } else {
      ret1 = (ret1 + num) % m;
      p1[num1++] = node(a - b, num);
      sum += a * num;
    }
  }
  if (ret1 + ret2 > m) {
    printf("%lld\n", sum);
    return 0;
  }
  sort(p1, p1 + num1, cmp);
  sort(p2, p2 + num2, cmp);
  for (int i = 0; i < num1; i++) {
    temp1 += min(p1[i].num, ret1) * p1[i].a;
    ret1 -= min(p1[i].num, ret1);
  }
  for (int i = 0; i < num2; i++) {
    temp2 += min(p2[i].num, ret2) * p2[i].a;
    ret2 -= min(p2[i].num, ret2);
  }
  sum -= min(temp1, temp2);
  printf("%lld\n", sum);
  return 0;
}
