#include <bits/stdc++.h>
const int N = 1e5 + 5;
const int mod = 1e9;
using namespace std;
struct node {
  int s, d;
} a[N], b[N];
bool cmp(node a, node b) { return a.d < b.d; }
bool cmp1(node a, node b) { return a.d > b.d; }
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  long long n, m, a8, b8, c, l1 = 0, l2 = 0, num = 0, d, ans = 0, p = 0, k = 0;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a8 >> b8 >> c;
    d = c - b8;
    ans += a8 * b8;
    k += a8;
    if (d > 0) {
      a[l1].s = a8;
      a[l1].d = d;
      ans += d * a[l1].s;
      num += a[l1].s;
      l1++;
    } else {
      b[l2].s = a8;
      b[l2].d = d;
      l2++;
    }
  }
  num %= m;
  if (num != 0) {
    if (k % m != 0) p = (k / m + 1) * m - k;
    m -= p;
    m -= num;
    sort(a, a + l1, cmp);
    sort(b, b + l2, cmp1);
    long long s, q;
    p = 0;
    while (num > 0) {
      for (int i = 0; i < l1; i++) {
        s = min(num, (long long)a[i].s);
        num -= s;
        p += s * a[i].d;
        if (num == 0) break;
      }
    }
    q = p;
    while (m > 0) {
      for (int i = 0; i < l2; i++) {
        s = min(m, (long long)b[i].s);
        m -= s;
        p += s * b[i].d;
        if (m == 0) break;
      }
    }
    if (p > 0)
      ans -= q - p;
    else
      ans -= q;
  }
  cout << ans;
}
