#include <bits/stdc++.h>
inline long long MAX2(long long a, long long int b) {
  return (a) > (b) ? (a) : (b);
}
inline long long MAX3(long long a, long long b, long long c) {
  return (a) > (b) ? ((a) > (c) ? (a) : (c)) : ((b) > (c) ? (b) : (c));
}
inline long long MIN2(long long a, long long b) {
  return (a) < (b) ? (a) : (b);
}
inline long long MIN3(long long a, long long b, long long c) {
  return (a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c));
}
using namespace std;
vector<vector<long long> > afav, bfav;
long long curr1 = 0, curr2 = 0, l, s;
bool compare(vector<long long> i, vector<long long> j) {
  if (abs(i[1] - i[2]) != abs(j[1] - j[2]))
    return (abs(i[1] - i[2]) > abs(j[1] - j[2]));
  else
    return i[1] > j[1];
}
long long give(long long &p, long long type1, long long q, long long type2) {
  int i;
  long long ans = 0;
  if (type1 == 0) {
    for (i = 0; i < afav.size(); i++) {
      long long temp = MIN2(p, afav[i][0]);
      if (temp < afav[i][0]) q -= afav[i][0] - temp;
      p -= temp;
      ans += temp * afav[i][1] + (afav[i][0] - temp) * afav[i][2];
      if (p == 0 && q == 0) return ans;
    }
  } else {
    for (i = 0; i < bfav.size(); i++) {
      long long temp = MIN2(p, bfav[i][0]);
      if (temp < bfav[i][0]) q -= bfav[i][0] - temp;
      p -= temp;
      ans += temp * bfav[i][2] + (bfav[i][0] - temp) * bfav[i][1];
      if (p == 0 && q == 0) return ans;
    }
  }
  return ans;
}
int main() {
  long long n, i, a, b, slice;
  cin >> n >> s;
  for (i = 0; i < n; i++) {
    vector<long long> temp;
    cin >> slice >> a >> b;
    if (a > b) {
      afav.push_back(temp);
      afav[afav.size() - 1].push_back(slice);
      afav[afav.size() - 1].push_back(a);
      afav[afav.size() - 1].push_back(b);
    } else {
      bfav.push_back(temp);
      bfav[bfav.size() - 1].push_back(slice);
      bfav[bfav.size() - 1].push_back(a);
      bfav[bfav.size() - 1].push_back(b);
    }
  }
  sort(afav.begin(), afav.end(), compare);
  sort(bfav.begin(), bfav.end(), compare);
  long long need = 0, ap, bp, ae, be;
  for (i = 0; i < afav.size(); i++) {
    need += afav[i][0];
  }
  ap = (need / s);
  ae = (need % s);
  need = 0;
  for (i = 0; i < bfav.size(); i++) {
    need += bfav[i][0];
  }
  bp = (need / s);
  be = (need % s);
  long long ans = 0;
  if (ae + be > s || ae + be == 0) {
    if (ae + be > s) {
      ap++;
      bp++;
    }
    long long slices = ap * s;
    ans += give(slices, 0, 0, 1);
    slices = bp * s;
    ans += give(slices, 1, 0, 0);
  } else {
    long long case1 = 0, case2 = 0;
    long long slices = (ap + 1) * s;
    case1 += give(slices, 0, 0, 1);
    long long slices2 = bp * s;
    case1 += give(slices2, 1, slices, 0);
    slices = (bp + 1) * s;
    case2 += give(slices, 1, 0, 0);
    slices2 = ap * s;
    case2 += give(slices2, 0, slices, 1);
    ans = MAX2(case1, case2);
  }
  cout << ans << endl;
}
