#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using lb = long double;
const ll mod = 1e9 + 7;
int a[200];
int main() {
  ll n, s;
  cin >> n >> s;
  ll a, b, c;
  vector<pair<ll, ll>> v1, v2;
  ll cnt1 = 0, cnt2 = 0, ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> a >> b >> c;
    if (b > c) {
      v1.push_back(make_pair(b - c, a));
      cnt1 += a;
      ans += a * b;
    } else {
      v2.push_back(make_pair(c - b, a));
      cnt2 += a;
      ans += a * c;
    }
  }
  if (cnt1 % s + cnt2 % s > s) {
    cout << ans << '\n';
  } else {
    ll t1 = cnt1 % s, t2 = cnt2 % s;
    ll p1 = 0, p2 = 0;
    sort(v1.begin(), v1.end());
    sort(v2.begin(), v2.end());
    for (int i = 0; t1; i++) {
      p1 += min(t1, v1[i].second) * v1[i].first;
      t1 -= min(t1, v1[i].second);
    }
    for (int i = 0; t2; i++) {
      p2 += min(t2, v2[i].second) * v2[i].first;
      t2 -= min(t2, v2[i].second);
    }
    cout << ans - min(p1, p2) << '\n';
  }
  return 0;
}
