#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[]) {
  int n, S, s, a, b;
  vector<pair<int, int> > v1, v2;
  scanf("%d %d", &n, &S);
  long long ans = 0, cnt1 = 0, cnt2 = 0, cnt3 = 0;
  for (int i = 0; i < n; ++i) {
    scanf("%d %d %d", &s, &a, &b);
    ans += 1LL * max(a, b) * s;
    if (a > b) {
      cnt1 += s;
      v1.push_back(make_pair(a - b, s));
    } else if (a < b) {
      cnt2 += s;
      v2.push_back(make_pair(b - a, s));
    } else
      cnt3 += s;
  }
  cnt1 %= S;
  cnt2 %= S;
  if (cnt1 + cnt2 + cnt3 <= S) {
    sort(v1.begin(), v1.end());
    sort(v2.begin(), v2.end());
    long long tmp1 = 0, tmp2 = 0;
    for (int i = 0; i < v1.size() && cnt1; ++i) {
      int num = min((int)cnt1, v1[i].second);
      tmp1 += 1LL * v1[i].first * num;
      cnt1 -= num;
    }
    for (int i = 0; i < v2.size() && cnt2; ++i) {
      int num = min((int)cnt2, v2[i].second);
      tmp2 += 1LL * v2[i].first * num;
      cnt2 -= num;
    }
    if (tmp1 < tmp2)
      ans -= tmp1;
    else
      ans -= tmp2;
  }
  printf("%lld\n", ans);
  return 0;
}
