#include <bits/stdc++.h>
using namespace std;
const int N = 100002;
long long a[N], b[N], s[N];
vector<pair<long long, int> > v1;
vector<pair<long long, int> > v2;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, S;
  cin >> n >> S;
  for (int i = 0; i < n; i++) {
    cin >> s[i] >> a[i] >> b[i];
  }
  long long t1 = 0, t2 = 0;
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] > b[i]) {
      ans += a[i] * s[i];
      t1 += s[i];
      v1.push_back(make_pair(a[i] - b[i], i));
    } else {
      ans += b[i] * s[i];
      t2 += s[i];
      v2.push_back(make_pair(b[i] - a[i], i));
    }
  }
  int extra = t1 % S + t2 % S;
  if (extra > S) {
    cout << ans << endl;
    return 0;
  }
  sort(v1.begin(), v1.end());
  sort(v2.begin(), v2.end());
  long long ans1 = ans;
  int idx = 0;
  int rem = t1 % S;
  for (int i = 0; i < rem; i++) {
    if (s[v1[idx].second]) {
      s[v1[idx].second]--;
      ans1 -= v1[idx].first;
    } else {
      idx++;
      s[v1[idx].second]--;
      ans1 -= v1[idx].first;
    }
  }
  long long ans2 = ans;
  idx = 0;
  rem = t2 % S;
  for (int i = 0; i < rem; i++) {
    if (s[v2[idx].second]) {
      s[v2[idx].second]--;
      ans2 -= v2[idx].first;
    } else {
      idx++;
      s[v2[idx].second]--;
      ans2 -= v2[idx].first;
    }
  }
  cout << max(ans1, ans2) << endl;
  return 0;
}
