#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e5 + 10;
struct node {
  long long x;
  long long id;
} c[maxn];
bool cmp(node a, node b) { return a.x < b.x; }
long long s1 = 0, s2 = 0, sum = 0, s[maxn];
long long a[maxn], b[maxn];
int main() {
  long long n, S, ans = 0;
  cin >> n >> S;
  for (long long i = 1; i <= n; ++i) {
    cin >> s[i] >> a[i] >> b[i];
    if (a[i] > b[i]) {
      s1 += s[i];
    }
    if (a[i] < b[i]) s2 += s[i];
    sum += s[i];
    c[i].x = a[i] - b[i];
    c[i].id = i;
    ans = ans + s[i] * max(a[i], b[i]);
  }
  long long cur = (sum + S - 1) / S;
  if ((s1 + S - 1) / S + (s2 + S - 1) / S <= cur) {
    cout << ans << endl;
    return 0;
  }
  s1 = s1 % S, s2 = s2 % S;
  sort(c + 1, c + n + 1, cmp);
  long long m = 0;
  long long a1 = 0, a2 = 0;
  for (long long i = 1; i <= n; ++i) {
    if (c[i].x <= 0) {
      m = i;
      continue;
    }
    if (!s1) break;
    long long t = min(s[c[i].id], s1);
    a1 += t * c[i].x;
    s1 -= t;
  }
  for (long long i = m; i >= 1; --i) {
    if (!c[i].x) continue;
    if (!s2) break;
    long long t = min(s[c[i].id], s2);
    a2 -= t * c[i].x;
    s2 -= t;
  }
  cout << ans - min(a1, a2) << endl;
}
