#include <bits/stdc++.h>
using namespace std;
int GLL(long long& x) { return scanf("%lld", &x); }
int GI(int& x) { return scanf("%d", &x); }
vector<tuple<long long, long long, long long, long long> > q;
int n, m;
long long calcscore(long long na, long long nb, bool verbose = false) {
  long long score = 0;
  int idx = q.size() - 1;
  long long count = 0;
  while (idx >= 0) {
    long long d = get<0>(q[idx]);
    long long v1 = get<1>(q[idx]);
    long long v2 = get<2>(q[idx]);
    long long c = get<3>(q[idx]);
    long long k;
    long long* nptr;
    long long v;
    if (v1 > v2 && na > 0) {
      v = v1;
      nptr = &na;
    } else if (v2 > v1 && nb > 0) {
      v = v2;
      nptr = &nb;
    } else {
      if (na > nb) {
        v = v1;
        nptr = &na;
      } else {
        v = v2;
        nptr = &nb;
      }
    }
    k = min(*nptr, c - count);
    score += v * k;
    *nptr -= k;
    count += k;
    if (c == count) {
      idx--;
      count = 0;
    }
  }
  return score;
}
int main() {
  GI(n);
  GI(m);
  long long totslice = 0;
  for (int i = 0; i < (int)(n); i++) {
    int c;
    GI(c);
    totslice += c;
    int t1, t2;
    GI(t1);
    GI(t2);
    q.push_back(make_tuple(abs(t1 - t2), t1, t2, c));
  }
  sort(q.begin(), q.end());
  long long npizza = totslice / m + (totslice % m > 0);
  long long score = 0;
  long long lo = 0;
  long long hi = npizza;
  while (lo <= hi) {
    long long mid = (lo + hi) / 2;
    long long r = min(mid + 1, npizza);
    long long smid = calcscore(mid * m, (npizza - mid) * m);
    long long sr = calcscore(r * m, (npizza - r) * m);
    score = max(score, sr);
    score = max(score, smid);
    if (sr > smid) {
      lo = mid + 1;
    } else if (sr < smid) {
      hi = mid - 1;
    } else {
      break;
    }
  }
  printf("%lld\n", score);
  return 0;
}
