#include <bits/stdc++.h>
const int mod = 1e3 + 5;
const long long inf = 0x3f3f3f3f3f3f3f;
const int maxn = 1e3 + 5;
using namespace std;
int n, s;
long long s1, s2, s3;
struct node {
  int c, x, y;
};
vector<node> sa, sb;
bool cmp(node a, node b) { return abs(a.x - a.y) < abs(b.x - b.y); }
long long ans;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> s;
  for (int i = 0; i < n; i++) {
    node tmp;
    cin >> tmp.c >> tmp.x >> tmp.y;
    s1 += tmp.c;
    if (tmp.x >= tmp.y)
      sa.push_back(tmp), s2 += tmp.c, ans += 1ll * tmp.c * tmp.x;
    else
      sb.push_back(tmp), s3 += tmp.c, ans += 1ll * tmp.c * tmp.y;
  }
  sort(sa.begin(), sa.end(), cmp);
  sort(sb.begin(), sb.end(), cmp);
  int t1 = s2 % s;
  int t2 = s3 % s;
  int cn1 = s - t2, cn2 = s - t1;
  long long an1 = ans, an2 = ans, an3 = ans, an4 = ans;
  if ((t1 + t2) > s) {
    cout << ans << endl;
    return 0;
  }
  for (int i = 0; i < sa.size(); i++) {
    if (t1 >= sa[i].c) {
      an3 -= 1ll * sa[i].c * abs(sa[i].x - sa[i].y);
      t1 -= sa[i].c;
    } else {
      an3 -= 1ll * t1 * abs(sa[i].x - sa[i].y);
      break;
    }
  }
  for (int i = 0; i < sa.size(); i++) {
    if (cn1 >= sa[i].c) {
      an1 -= 1ll * sa[i].c * abs(sa[i].x - sa[i].y);
      cn1 -= sa[i].c;
    } else {
      an1 -= 1ll * cn1 * abs(sa[i].x - sa[i].y);
      break;
    }
  }
  for (int i = 0; i < sb.size(); i++) {
    if (t2 >= sb[i].c) {
      an4 -= 1ll * sb[i].c * abs(sb[i].x - sb[i].y);
      t2 -= sb[i].c;
    } else {
      an4 -= 1ll * t2 * abs(sb[i].x - sb[i].y);
      break;
    }
  }
  for (int i = 0; i < sb.size(); i++) {
    if (cn2 >= sb[i].c) {
      an2 -= 1ll * sb[i].c * abs(sb[i].x - sb[i].y);
      cn2 -= sb[i].c;
    } else {
      an2 -= 1ll * cn2 * abs(sb[i].x - sb[i].y);
      break;
    }
  }
  cout << max(max(an1, an2), max(an3, an4)) << endl;
  return 0;
}
