#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
struct node {
  long long s, a, b, c;
} t1[maxn], t2[maxn];
bool cmp(node x, node y) { return x.c < y.c; }
int main() {
  long long n, S, n1 = 0, n2 = 0, sum1 = 0, sum2 = 0, ans = 0, ans1 = 0,
                  ans2 = 0, x, y, z;
  cin >> n >> S;
  for (int i = 0; i < n; i++) {
    cin >> x >> y >> z;
    if (y > z) {
      t1[n1].s = x;
      t1[n1].a = y;
      t1[n1].b = z;
      t1[n1].c = y - z;
      ans += t1[n1].a * t1[n1].s;
      sum1 = (sum1 + t1[n1].s) % S;
      n1++;
    } else {
      t2[n2].s = x;
      t2[n2].a = y;
      t2[n2].b = z;
      t2[n2].c = z - y;
      ans += t2[n2].b * t2[n2].s;
      sum2 = (sum2 + t2[n2].s) % S;
      n2++;
    }
  }
  if (sum1 + sum2 > S) {
    cout << ans << endl;
  } else {
    sort(t1, t1 + n1, cmp);
    for (int i = 0; i < n1; i++) {
      ans1 += min(sum1, t1[i].s) * t1[i].c;
      sum1 -= min(sum1, t1[i].s);
    }
    sort(t2, t2 + n2, cmp);
    for (int i = 0; i < n2; i++) {
      ans2 += min(sum2, t2[i].s) * t2[i].c;
      sum2 -= min(sum2, t2[i].s);
    }
    if (ans1 < ans2)
      ans -= ans1;
    else
      ans -= ans2;
    cout << ans << endl;
  }
  return 0;
}
