#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  long long n, s;
  cin >> n >> s;
  long long sum = 0, t1 = 0, t2 = 0;
  vector<pair<long long, long long> > v, u;
  for (long long i = (long long)(1); i <= (long long)(n); i++) {
    long long x, y, z;
    cin >> z >> x >> y;
    sum += z * max(x, y);
    if (x >= y) v.push_back({x - y, z}), t1 += z;
    if (y > x) u.push_back({y - x, z}), t2 += z;
  }
  long long p = t1 % s, q = t2 % s;
  if (p == 0 || q == 0 || p + q > s) cout << sum << "\n", exit(0);
  ;
  sort(v.begin(), v.end());
  sort(u.begin(), u.end());
  long long s1 = 0, s2 = 0;
  for (long long i = (long long)(0); i <= (long long)(v.size() - 1); i++) {
    if (v[i].second < p) {
      s1 += v[i].first * v[i].second;
      p -= v[i].second;
    } else {
      s1 += v[i].first * p;
      break;
    }
  }
  for (long long i = (long long)(0); i <= (long long)(u.size() - 1); i++) {
    if (u[i].second < q) {
      s2 += u[i].first * u[i].second;
      q -= u[i].second;
    } else {
      s2 += u[i].first * q;
      break;
    }
  }
  cout << sum - min(s1, s2);
}
