#include <bits/stdc++.h>   using namespace std;   const int maxn = 100005;   int n, s;   long long a[maxn], b[maxn], c[maxn];   vector<int> p[2];   bool cmp1(int i, int j) { return b[i] - c[i] > b[j] - c[j]; }   bool cmp2(int i, int j) { return c[i] - b[i] > c[j] - b[j]; }   int main() {     scanf( %d%d , &n, &s);     long long tot = 0, A = 0, B = 0, v1 = 0, v2 = 0;     p[0].clear();     p[1].clear();     for (int i = 0; i < n; i++) {       scanf( %I64d%I64d%I64d , a + i, b + i, c + i);       tot += a[i];       if (b[i] > c[i]) {         p[0].push_back(i);         A += a[i] * b[i];         v1 += a[i];       } else {         p[1].push_back(i);         B += a[i] * c[i];         v2 += a[i];       }     }     sort(p[0].begin(), p[0].end(), cmp1);     sort(p[1].begin(), p[1].end(), cmp2);     long long ans = 0;     long long c1 = v1 / s + (v1 % s ? 1 : 0);     long long c2 = v2 / s + (v2 % s ? 1 : 0);     tot = tot / s + (tot % s ? 1 : 0);     if (c1 + c2 > tot) {       long long tmp = A + B;       long long now = v1 % s;       for (int i = p[0].size() - 1; i >= 0; i--) {         now -= a[p[0][i]];         long long cnt = a[p[0][i]];         if (now < 0) cnt = a[p[0][i]] + now;         tmp += (c[p[0][i]] - b[p[0][i]]) * cnt;         if (now <= 0) break;       }       ans = max(ans, tmp);       tmp = A + B;       now = v2 % s;       for (int i = p[1].size() - 1; i >= 0; i--) {         now -= a[p[1][i]];         long long cnt = a[p[1][i]];         if (now < 0) cnt = a[p[1][i]] + now;         tmp += (b[p[1][i]] - c[p[1][i]]) * cnt;         if (now <= 0) break;       }       ans = max(ans, tmp);     } else       ans = A + B;     printf( %I64d n , ans);     return 0;   }   