#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int cc[N], a[N], b[N];
pair<int, int> A[N], B[N];
long long getMini(pair<int, int> *in, int sz, int rem) {
  long long ret = 0;
  priority_queue<pair<int, int> > pq;
  for (int i = 0; i < sz; ++i) {
    pq.push(in[i]);
  }
  while (rem > 0 && !pq.empty()) {
    long long val = pq.top().first;
    int id = pq.top().second;
    long long x = cc[id];
    long long need = min((long long)rem, x);
    rem -= need;
    ret += need * val;
    pq.pop();
  }
  return ret;
}
void solve() {
  int n, S;
  scanf("%d%d", &n, &S);
  int asz = 0, bsz = 0;
  long long aSum = 0, bSum = 0;
  long long tot = 0;
  for (int i = 0; i < n; ++i) {
    scanf("%d%d%d", &cc[i], &a[i], &b[i]);
    if (a[i] >= b[i]) {
      A[asz++] = make_pair(b[i] - a[i], i);
      aSum += cc[i];
      tot += (long long)cc[i] * a[i];
    } else {
      B[bsz++] = make_pair(a[i] - b[i], i);
      bSum += cc[i];
      tot += (long long)cc[i] * b[i];
    }
  }
  if (aSum % S + bSum % S > S) {
    printf("%lld\n", tot);
  } else {
    long long ret =
        tot + max(getMini(A, asz, aSum % S), getMini(B, bsz, bSum % S));
    printf("%lld\n", ret);
  }
}
int main() { solve(); }
