#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007LL;
int n, d;
int s[100005], a[100005], b[100005];
int main() {
  cin >> n >> d;
  priority_queue<pair<int, int> > q1, q2;
  long long ans = 0LL;
  long long n1 = 0, n2 = 0;
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d%d", &s[i], &a[i], &b[i]);
    if (a[i] > b[i]) {
      ans += (long long)a[i] * (long long)s[i];
      n1 += (long long)s[i];
      q1.push(make_pair(b[i] - a[i], s[i]));
    } else {
      ans += (long long)b[i] * (long long)s[i];
      n2 += (long long)s[i];
      q2.push(make_pair(a[i] - b[i], s[i]));
    }
  }
  n1 %= d;
  n2 %= d;
  if (n1 + n2 > d) {
    printf("%lld\n", ans);
    return 0;
  }
  long long c1 = 0, c2 = 0;
  while (!q1.empty()) {
    auto top = q1.top();
    q1.pop();
    if (top.second >= n1) {
      c1 += n1 * top.first;
      break;
    } else {
      c1 += (long long)top.second * (long long)top.first;
      n1 -= top.second;
    }
  }
  while (!q2.empty()) {
    auto top = q2.top();
    q2.pop();
    if (top.second >= n2) {
      c2 += n2 * top.first;
      break;
    } else {
      c2 += (long long)top.second * (long long)top.first;
      n2 -= top.second;
    }
  }
  printf("%lld\n", max(ans + c1, ans + c2));
  return 0;
}
