#include <bits/stdc++.h>
using namespace std;
long long int pwr(long long int base, long long int p, long long int mod) {
  long long int ans = 1;
  while (p) {
    if (p & 1) ans = (ans * base) % mod;
    base = (base * base) % mod;
    p /= 2;
  }
  return ans;
}
long long int gcd(long long int a, long long int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
struct node {
  long long int l, h;
};
bool comp(node a, node b) { return a.h < b.h; }
int main() {
  long long int n, m;
  scanf("%lld", &n);
  scanf("%lld", &m);
  vector<node> v1;
  vector<node> v2;
  long long int first = 0, second = 0, c1 = 0, c2 = 0;
  for (long long int i = 0; i < n; i++) {
    long long int s, a, b;
    scanf("%lld", &s);
    scanf("%lld", &a);
    scanf("%lld", &b);
    if (a > b) {
      c1 += s;
      first += a * s;
      v1.push_back({s, a - b});
    } else {
      c2 += s;
      second += b * s;
      v2.push_back({s, b - a});
    }
  }
  if ((c1 % m) + (c2 % m) > m) {
    printf("%lld\n", first + second);
    return 0;
  }
  long long int ans = first + second;
  sort(v1.begin(), v1.end(), comp);
  sort(v2.begin(), v2.end(), comp);
  long long int x = c1 % m;
  long long int l = v1.size();
  long long int ans1 = ans;
  for (int i = 0; i < l; i++) {
    long long int r = min(v1[i].l, x);
    ans1 -= (r * v1[i].h);
    x -= r;
    if (x <= 0) break;
  }
  x = c2 % m;
  l = v2.size();
  long long int ans2 = ans;
  for (int i = 0; i < l; i++) {
    long long int r = min(v2[i].l, x);
    ans2 -= (r * v2[i].h);
    x -= r;
    if (x <= 0) break;
  }
  printf("%lld\n", max(ans1, ans2));
  return 0;
}
