#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100005;
int main() {
  int N, S;
  long long int sum1, sum2;
  long long int ans;
  pair<long long int, long long int> pair1[MAX_N];
  pair<long long int, long long int> pair2[MAX_N];
  while (cin >> N >> S) {
    sum1 = 0;
    sum2 = 0;
    ans = 0;
    long long int temp1 = 0, temp2 = 0;
    int count1 = 0, count2 = 0;
    for (int i = 0; i < N; i++) {
      long long int s, a, b;
      cin >> s >> a >> b;
      if (a > b) {
        pair1[count1].first = a - b;
        pair1[count1].second = s;
        sum1 += s;
        ans += s * a;
        count1++;
      } else {
        pair2[count2].first = b - a;
        pair2[count2].second = s;
        sum2 += s;
        ans += s * b;
        count2++;
      }
    }
    sum1 %= S;
    sum2 %= S;
    if (sum1 + sum2 > S)
      cout << ans << endl;
    else {
      sort(pair1, pair1 + count1);
      sort(pair2, pair2 + count2);
      for (int i = 0; i < count1; i++) {
        temp1 += min(sum1, pair1[i].second) * pair1[i].first;
        sum1 -= min(sum1, pair1[i].second);
        if (sum1 == 0) break;
      }
      for (int i = 0; i < count2; i++) {
        temp2 += min(sum2, pair2[i].second) * pair2[i].first;
        sum2 -= min(sum2, pair2[i].second);
        if (sum2 == 0) break;
      }
      cout << ans - min(temp1, temp2) << endl;
    }
  }
  return 0;
}
