#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 23;
const int delta = (int)1e9 + 7;
long long a[(int)1e5 + 23], b[(int)1e5 + 23], x[(int)1e5 + 23];
vector<pair<long long, long long> > A, B, C;
vector<pair<long long, pair<long long, long long> > > D;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long n, s;
  cin >> n >> s;
  long long num = 0;
  long long a1 = 0, a2 = 0, a3 = 0;
  long long ans1 = 0, ans2 = 0, ans3 = 0;
  for (long long i = 1; i <= n; i++) {
    cin >> x[i] >> a[i] >> b[i];
    num += x[i];
    if (a[i] > b[i]) {
      A.push_back(pair<long long, long long>((a[i] - b[i]), i));
      D.push_back(pair<long long, pair<long long, long long> >(
          a[i] - b[i], pair<long long, long long>(i, 1)));
      a1 += x[i];
      ans1 += x[i] * a[i];
    } else if (a[i] == b[i]) {
      C.push_back(pair<long long, long long>(a[i], i));
      ans3 += a[i] * x[i];
      a3 += x[i];
    } else {
      B.push_back(pair<long long, long long>((b[i] - a[i]), i));
      D.push_back(pair<long long, pair<long long, long long> >(
          a[i] - b[i], pair<long long, long long>(i, 2)));
      ans2 += b[i] * x[i];
      a2 += x[i];
    }
  }
  long long ans;
  ans = num / s;
  if (num % s != 0) {
    ans++;
  }
  long long n1, n2, n3;
  n1 = a1 / s;
  if (a1 % s != 0) {
    n1++;
  }
  n2 = a2 / s;
  if (a2 % s != 0) {
    n2++;
  }
  long long s1 = s;
  long long ansl;
  long long ansr = 0;
  ansl = ans1 + ans2 + ans3;
  if (n1 + n2 <= ans) {
    cout << ans1 + ans2 + ans3;
    return 0;
  } else {
    s = a1 % s1;
    long long p = 0, q = s;
    sort(A.begin(), A.end());
    sort(B.begin(), B.end());
    sort(D.begin(), D.end());
    n1--;
    for (long long i = 0; i < A.size(); i++) {
      if (p + x[A[i].second] < s) {
        ansl -= A[i].first * x[A[i].second];
        p += x[A[i].second];
        q -= x[A[i].second];
      } else {
        ansl -= A[i].first * q;
        p += q;
      }
      if (p >= s) {
        break;
      }
    }
    ansr = max(ansr, ansl);
    n1++;
    n2--;
    s = a2 % s1;
    p = 0;
    q = s;
    ansl = ans1 + ans2 + ans3;
    for (long long i = 0; i < B.size(); i++) {
      if (p + x[B[i].second] < s) {
        ansl -= B[i].first * x[B[i].second];
        p += x[B[i].second];
        q -= x[B[i].second];
      } else {
        ansl -= B[i].first * q;
        p += q;
      }
      if (p >= s) {
        break;
      }
    }
    ansr = max(ansr, ansl);
  }
  cout << ansr;
  return 0;
}
