#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;
using vi = vector<int>;
using vii = vector<ii>;
using vs = vector<string>;
const double PI = acos(-1.0);
const double EPS = 1e-14;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const ll INFLL = 4e18;
const int MAX = 1e5;
struct person {
  int slice, a, b;
};
int N, S;
person arr[MAX + 5];
ll happy, need_pizza, slice_A, slice_B;
void read() {
  cin >> N >> S;
  for (int i = 0; i < (int)N; i++) {
    cin >> arr[i].slice >> arr[i].a >> arr[i].b;
  }
}
void solve() {
  priority_queue<ii> pqA, pqB;
  for (int i = 0; i < (int)N; i++) {
    if (arr[i].b > arr[i].a) {
      slice_B += arr[i].slice;
      happy += 1LL * arr[i].slice * arr[i].b;
      pqB.push({arr[i].a - arr[i].b, i});
    } else {
      slice_A += arr[i].slice;
      happy += 1LL * arr[i].slice * arr[i].a;
      pqA.push({arr[i].b - arr[i].a, i});
    }
    need_pizza += arr[i].slice;
  }
  need_pizza = ceil(1.0 * need_pizza / S);
  assert((ceil(1.0 * slice_A / S) + ceil(1.0 * slice_B / S) - need_pizza) <= 1);
  if (ceil(1.0 * slice_A / S) + ceil(1.0 * slice_B / S) > need_pizza) {
    int needA = slice_A % S, needB = slice_B % S;
    ll minA = 0, minB = 0;
    while (needA > 0) {
      int diff = pqA.top().first;
      int cur = pqA.top().second;
      pqA.pop();
      minA += 1LL * min(needA, arr[cur].slice) * diff;
      needA -= min(needA, arr[cur].slice);
    }
    while (needB > 0) {
      int diff = pqB.top().first;
      int cur = pqB.top().second;
      pqB.pop();
      minB += 1LL * min(needB, arr[cur].slice) * diff;
      needB -= min(needB, arr[cur].slice);
    }
    happy += max(minA, minB);
  }
  cout << happy << "\n";
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int TC = 1;
  while (TC--) {
    read();
    solve();
  }
}
