#include <bits/stdc++.h>
using namespace std;
bool cmp(array<long long int, 3> a1, array<long long int, 3> a2) {
  return ((a1[2] - a1[1]) > (a2[2] - a2[1]));
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int n, s;
  cin >> n >> s;
  vector<array<long long int, 3>> v;
  long long int ans = 0;
  long long int ts = 0;
  for (int i = 0; i < n; i++) {
    long long int si, a, b;
    cin >> si >> a >> b;
    v.push_back({si, a, b});
    ans += si * a;
    ts += si;
  }
  long long int x = s * ((ts / s) + (ts % s != 0)) - ts;
  v.push_back({x, 0, 0});
  n = v.size();
  sort(v.begin(), v.end(), cmp);
  int i = 0, j = 0;
  while (i < n) {
    if (v[i][0] == 0) {
      i++;
      continue;
    }
    x = 0;
    long long int a1 = 0;
    if (v[i][0] > s) {
      x = ((v[i][0] / s) * s);
      a1 = x * (v[i][2] - v[i][1]);
      v[i][0] -= x;
    } else {
      while (x + v[j][0] < s) {
        x += v[j][0];
        a1 += v[j][0] * (v[j][2] - v[j][1]);
        j++;
      }
      a1 += (s - x) * (v[j][2] - v[j][1]);
      v[j][0] -= (s - x);
      i = j;
    }
    ans = max(ans, ans + a1);
    if (a1 < 0) {
      break;
    }
  }
  cout << ans << '\n';
  return 0;
}
