#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const long long INF = 1e18;
pair<int, int> diffa[maxn];
pair<int, int> diffb[maxn];
long long s[maxn], a[maxn], b[maxn];
int main() {
  int N, S;
  cin >> N >> S;
  long long ans = 0;
  long long sum = 0;
  long long suma = 0;
  long long sumb = 0;
  int idxa = 0;
  int idxb = 0;
  for (int i = 0; i < N; i++) {
    scanf("%d%d%d", &s[i], &a[i], &b[i]);
    if (a[i] >= b[i]) {
      ans += s[i] * a[i];
      suma += s[i];
      diffa[idxa++] = pair<int, int>(a[i] - b[i], i);
    } else {
      ans += s[i] * b[i];
      sumb += s[i];
      diffb[idxb++] = pair<int, int>(b[i] - a[i], i);
    }
    sum += s[i];
  }
  sort(diffa, diffa + idxa);
  sort(diffb, diffb + idxb);
  if ((suma % S) + (sumb % S) > S) {
    cout << ans << endl;
  } else {
    long long numa = suma % S;
    long long numb = sumb % S;
    long long ans1 = ans;
    for (int i = 0; i < idxa; i++) {
      if (numa > s[diffa[i].second]) {
        numa -= s[diffa[i].second];
        ans1 -= s[diffa[i].second] * diffa[i].first;
      } else {
        ans1 -= numa * diffa[i].first;
        break;
      }
    }
    long long ans2 = ans;
    for (int i = 0; i < idxb; i++) {
      if (numb > s[diffb[i].second]) {
        numb -= s[diffb[i].second];
        ans2 -= s[diffb[i].second] * diffb[i].first;
      } else {
        ans2 -= numb * diffb[i].first;
        break;
      }
    }
    cout << max(ans1, ans2) << endl;
  }
}
