#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 50;
struct node {
  long long x, y;
  bool operator<(const node rhs) const { return x < rhs.x; }
} g1[N], g2[N];
long long id[N], n, m, T, cnt1, cnt2, s, first, second;
long long ans;
int main() {
  ios::sync_with_stdio(false);
  while (cin >> n >> s) {
    ans = first = second = 0;
    for (long long sum, x, y, i = 1; i <= n; i++) {
      cin >> sum >> x >> y;
      if (x >= y) {
        first += sum;
        first %= s;
        ans += sum * x;
        g1[++cnt1].x = abs(x - y);
        g1[cnt1].y = sum;
      } else {
        second += sum;
        second %= s;
        ans += sum * y;
        g2[++cnt2].x = abs(x - y);
        g2[cnt2].y = sum;
      }
    }
    if (first + second <= s) {
      sort(g1 + 1, g1 + cnt1 + 1);
      sort(g2 + 1, g2 + cnt2 + 1);
      int k = 1;
      long long a = ans, b = ans;
      while (first > 0) {
        a -= min(first, g1[k].y) * g1[k].x;
        first -= min(first, g1[k].y);
        k++;
      }
      k = 1;
      while (second > 0) {
        b -= min(second, g2[k].y) * g2[k].x;
        second -= min(second, g2[k].y);
        k++;
      }
      ans = max(a, b);
    }
    cout << ans << endl;
  }
  return 0;
}
