#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
struct Node {
  long long s, a, b, c;
} p[N];
bool cmp(const Node &x, const Node &y) { return x.c > y.c; }
int main() {
  int n;
  long long k, sum = 0, cas = 0;
  scanf("%d%lld", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%lld%lld%lld", &p[i].s, &p[i].a, &p[i].b);
    p[i].c = p[i].a - p[i].b;
    cas += p[i].s;
  }
  sort(p + 1, p + 1 + n, cmp);
  sum = (cas + k - 1LL) / k;
  long long ans1 = 0, ans2 = 0, ans3 = 0, ret = 0;
  for (int i = 1; i <= n; i++)
    if (p[i].c > 0)
      ans1 += p[i].s;
    else if (p[i].c == 0)
      ans2 += p[i].s;
    else if (p[i].c < 0)
      ans3 += p[i].s;
  long long cnt1 = (ans1 + k - 1) / k;
  long long cnt3 = (ans3 + k - 1) / k;
  if (cnt1 + cnt3 <= sum)
    for (int i = 1; i <= n; i++) ret += (p[i].s * (max(p[i].a, p[i].b)));
  else {
    cnt1--, cnt3--;
    long long num1 = cnt1 * k, num3 = cnt3 * k;
    long long num2 = cas - num1 - num3, asd1 = 0, asd2 = 0;
    bool f = true;
    for (int i = 1; i <= n; i++)
      if (num1 >= 0 && f)
        if (p[i].s <= num1)
          num1 -= p[i].s, ret += p[i].s * p[i].a;
        else {
          p[i].s -= num1;
          ret += num1 * p[i].a;
          f = false;
          for (int j = 0; j < (int)num2; j++) {
            if (p[i].s == 0) i++;
            asd1 += p[i].a;
            asd2 += p[i].b;
            p[i].s--;
          }
        }
    for (int i = n; i > 0; i--)
      if (num3 > 0)
        if (p[i].s <= num3)
          num3 -= p[i].s, ret += p[i].s * p[i].b;
        else
          ret += num3 * p[i].b, num3 = 0;
    ret += max(asd1, asd2);
  }
  return 0 * printf("%lld\n", ret);
}
