#include <bits/stdc++.h>
using namespace std;
long long a[500005], b[500005], c[500005];
struct node {
  long long cha, num;
} p[500005], q[500005];
bool cmp(const node &a, const node &b) { return a.cha < b.cha; }
int main() {
  long long n, s, num1 = 0, num2 = 0, ans = 0;
  cin >> n >> s;
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i] >> c[i];
    if (b[i] > c[i])
      p[++p[0].num].cha = b[i] - c[i], num1 += a[i], p[p[0].num].num = a[i],
      ans += b[i] * a[i];
    else
      q[++q[0].num].cha = c[i] - b[i], num2 += a[i], q[q[0].num].num = a[i],
      ans += c[i] * a[i];
  }
  num1 %= s, num2 %= s;
  if (num1 + num2 > s) {
    cout << ans << endl;
    return 0;
  }
  sort(p + 1, p + p[0].num + 1, cmp);
  sort(q + 1, q + q[0].num + 1, cmp);
  long long ans1 = 0, ans2 = 0;
  for (int i = 1; i <= p[0].num; i++) {
    ans1 += min(num1, p[i].num) * p[i].cha, num1 -= min(num1, p[i].num);
    if (num1 == 0) break;
  }
  for (int i = 1; i <= q[0].num; i++) {
    ans2 += min(num2, q[i].num) * q[i].cha, num2 -= min(num2, q[i].num);
    if (num2 == 0) break;
  }
  cout << ans - min(ans1, ans2) << endl;
  return 0;
}
