#include <bits/stdc++.h>
using namespace std;
long long N, second;
vector<long long> s, a, b;
vector<pair<long long, long long> > dela, delb;
void ini() {
  s.resize(N, 0);
  a.resize(N, 0);
  b.resize(N, 0);
}
long long cntPizza(long long a, long long b) {
  return ceil((double)a / second) + ceil((double)b / second);
}
signed main() {
  cin >> N >> second;
  ini();
  for (long long i = (0); i < (N); i++) cin >> s[i] >> a[i] >> b[i];
  long long ans = 0;
  long long cntA = 0, cntB = 0;
  for (long long i = (0); i < (N); i++) {
    if (a[i] >= b[i]) {
      dela.push_back({a[i] - b[i], s[i]});
      cntA += s[i];
    } else {
      delb.push_back({b[i] - a[i], s[i]});
      cntB += s[i];
    }
    ans += max(a[i], b[i]) * s[i];
  }
  sort(dela.begin(), dela.end());
  sort(delb.begin(), delb.end());
  long long piz = ceil((double)(cntA + cntB) / second);
  if (cntPizza(cntA, cntB) == piz) {
    cout << ans << '\n';
    return 0;
  }
  long long rem = cntA % second;
  long long cur = 0;
  long long del = 0;
  for (pair<long long, long long> X : dela) {
    if (cur + X.second > rem) {
      del -= X.first * (rem - cur);
      break;
    }
    del -= X.first * X.second;
    cur += X.second;
  }
  long long ansA = ans + del;
  cur = del = 0;
  rem = cntB % second;
  for (pair<long long, long long> X : delb) {
    if (cur + X.second > rem) {
      del -= X.first * (rem - cur);
      break;
    }
    del -= X.first * X.second;
    cur += X.second;
  }
  long long ansB = ans + del;
  cout << max(ansA, ansB) << '\n';
}
