#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const int UNDEF = -1;
const long long INF = 1e18;
template <typename T>
inline bool chkmax(T &aa, T bb) {
  return aa < bb ? aa = bb, true : false;
}
template <typename T>
inline bool chkmin(T &aa, T bb) {
  return aa > bb ? aa = bb, true : false;
}
int rint();
char rch();
long long rlong();
const int mn = 1e5 + 4;
vector<pair<long long, long long> > v[2];
long long vs[mn], va[mn], vb[mn];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long n = rint(), sperp = rint();
  long long alls = 0;
  long long base = 0;
  for (int i = 0; i < n; i++) {
    vs[i] = rint(), va[i] = rint(), vb[i] = rint();
    if (va[i] > vb[i]) {
      v[0].push_back(make_pair(va[i] - vb[i], vs[i]));
    } else if (vb[i] > va[i]) {
      v[1].push_back(make_pair(vb[i] - va[i], vs[i]));
    }
    base += min(va[i], vb[i]) * (long long)vs[i];
    alls += vs[i];
  }
  for (int t = 0; t < 2; t++) {
    sort(v[t].begin(), v[t].end());
    reverse(v[t].begin(), v[t].end());
  }
  long long wanta = 0;
  for (auto &w : v[0]) {
    wanta += w.second;
  }
  long long totp = (alls + sperp - 1) / sperp;
  long long wantap = wanta / sperp;
  long long final = 0;
  for (long long canda = max(0ll, wantap - 2); canda <= min(totp, wantap + 2);
       canda++) {
    long long candb = totp - canda;
    if (0 <= candb && candb <= totp) {
      long long pa = canda * sperp, pb = candb * sperp;
      long long gain = 0;
      for (auto &w : v[0]) {
        long long take = min(pa, w.second);
        gain += take * (long long)w.first;
        pa -= take;
      }
      for (auto &w : v[1]) {
        long long take = min(pb, w.second);
        gain += take * (long long)w.first;
        pb -= take;
      }
      chkmax(final, gain);
    }
  }
  final += base;
  printf("%lld\n", final);
}
static char stdinBuffer[1024];
static char *stdinDataEnd = stdinBuffer + sizeof(stdinBuffer);
static const char *stdinPos = stdinDataEnd;
void readAhead(size_t amount) {
  size_t remaining = stdinDataEnd - stdinPos;
  if (remaining < amount) {
    memmove(stdinBuffer, stdinPos, remaining);
    size_t sz = fread(stdinBuffer + remaining, 1,
                      sizeof(stdinBuffer) - remaining, stdin);
    stdinPos = stdinBuffer;
    stdinDataEnd = stdinBuffer + remaining + sz;
    if (stdinDataEnd != stdinBuffer + sizeof(stdinBuffer)) *stdinDataEnd = 0;
  }
}
int rint() {
  readAhead(16);
  int x = 0;
  bool neg = false;
  while (*stdinPos == ' ' || *stdinPos == '\n') ++stdinPos;
  if (*stdinPos == '-') {
    ++stdinPos;
    neg = true;
  }
  while (*stdinPos >= '0' && *stdinPos <= '9') {
    x *= 10;
    x += *stdinPos - '0';
    ++stdinPos;
  }
  return neg ? -x : x;
}
char rch() {
  readAhead(16);
  while (*stdinPos == ' ' || *stdinPos == '\n') ++stdinPos;
  char ans = *stdinPos;
  ++stdinPos;
  return ans;
}
long long rlong() {
  readAhead(32);
  long long x = 0;
  bool neg = false;
  while (*stdinPos == ' ' || *stdinPos == '\n') ++stdinPos;
  if (*stdinPos == '-') {
    ++stdinPos;
    neg = true;
  }
  while (*stdinPos >= '0' && *stdinPos <= '9') {
    x *= 10;
    x += *stdinPos - '0';
    ++stdinPos;
  }
  return neg ? -x : x;
}

### Prompt

Generate a cpp solution to the following problem:

It's another Start[c]up finals, and that means there is pizza to order for the onsite contestants. There are only 2 types of pizza (obviously not, but let's just pretend for the sake of the problem), and all pizzas contain exactly S slices.

It is known that the i-th contestant will eat si slices of pizza, and gain ai happiness for each slice of type 1 pizza they eat, and bi happiness for each slice of type 2 pizza they eat. We can order any number of type 1 and type 2 pizzas, but we want to buy the minimum possible number of pizzas for all of the contestants to be able to eat their required number of slices. Given that restriction, what is the maximum possible total happiness that can be achieved?

Input

The first line of input will contain integers N and S (1 ≤ N ≤ 105, 1 ≤ S ≤ 105), the number of contestants and the number of slices per pizza, respectively. N lines follow.

The i-th such line contains integers si, ai, and bi (1 ≤ si ≤ 105, 1 ≤ ai ≤ 105, 1 ≤ bi ≤ 105), the number of slices the i-th contestant will eat, the happiness they will gain from each type 1 slice they eat, and the happiness they will gain from each type 2 slice they eat, respectively.

Output

Print the maximum total happiness that can be achieved.

Examples

Input

3 12
3 5 7
4 6 7
5 9 5


Output

84


Input

6 10
7 4 7
5 8 8
12 5 8
6 11 6
3 3 7
5 9 6


Output

314

Note

In the first example, you only need to buy one pizza. If you buy a type 1 pizza, the total happiness will be 3·5 + 4·6 + 5·9 = 84, and if you buy a type 2 pizza, the total happiness will be 3·7 + 4·7 + 5·5 = 74.

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const int UNDEF = -1;
const long long INF = 1e18;
template <typename T>
inline bool chkmax(T &aa, T bb) {
  return aa < bb ? aa = bb, true : false;
}
template <typename T>
inline bool chkmin(T &aa, T bb) {
  return aa > bb ? aa = bb, true : false;
}
int rint();
char rch();
long long rlong();
const int mn = 1e5 + 4;
vector<pair<long long, long long> > v[2];
long long vs[mn], va[mn], vb[mn];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long n = rint(), sperp = rint();
  long long alls = 0;
  long long base = 0;
  for (int i = 0; i < n; i++) {
    vs[i] = rint(), va[i] = rint(), vb[i] = rint();
    if (va[i] > vb[i]) {
      v[0].push_back(make_pair(va[i] - vb[i], vs[i]));
    } else if (vb[i] > va[i]) {
      v[1].push_back(make_pair(vb[i] - va[i], vs[i]));
    }
    base += min(va[i], vb[i]) * (long long)vs[i];
    alls += vs[i];
  }
  for (int t = 0; t < 2; t++) {
    sort(v[t].begin(), v[t].end());
    reverse(v[t].begin(), v[t].end());
  }
  long long wanta = 0;
  for (auto &w : v[0]) {
    wanta += w.second;
  }
  long long totp = (alls + sperp - 1) / sperp;
  long long wantap = wanta / sperp;
  long long final = 0;
  for (long long canda = max(0ll, wantap - 2); canda <= min(totp, wantap + 2);
       canda++) {
    long long candb = totp - canda;
    if (0 <= candb && candb <= totp) {
      long long pa = canda * sperp, pb = candb * sperp;
      long long gain = 0;
      for (auto &w : v[0]) {
        long long take = min(pa, w.second);
        gain += take * (long long)w.first;
        pa -= take;
      }
      for (auto &w : v[1]) {
        long long take = min(pb, w.second);
        gain += take * (long long)w.first;
        pb -= take;
      }
      chkmax(final, gain);
    }
  }
  final += base;
  printf("%lld\n", final);
}
static char stdinBuffer[1024];
static char *stdinDataEnd = stdinBuffer + sizeof(stdinBuffer);
static const char *stdinPos = stdinDataEnd;
void readAhead(size_t amount) {
  size_t remaining = stdinDataEnd - stdinPos;
  if (remaining < amount) {
    memmove(stdinBuffer, stdinPos, remaining);
    size_t sz = fread(stdinBuffer + remaining, 1,
                      sizeof(stdinBuffer) - remaining, stdin);
    stdinPos = stdinBuffer;
    stdinDataEnd = stdinBuffer + remaining + sz;
    if (stdinDataEnd != stdinBuffer + sizeof(stdinBuffer)) *stdinDataEnd = 0;
  }
}
int rint() {
  readAhead(16);
  int x = 0;
  bool neg = false;
  while (*stdinPos == ' ' || *stdinPos == '\n') ++stdinPos;
  if (*stdinPos == '-') {
    ++stdinPos;
    neg = true;
  }
  while (*stdinPos >= '0' && *stdinPos <= '9') {
    x *= 10;
    x += *stdinPos - '0';
    ++stdinPos;
  }
  return neg ? -x : x;
}
char rch() {
  readAhead(16);
  while (*stdinPos == ' ' || *stdinPos == '\n') ++stdinPos;
  char ans = *stdinPos;
  ++stdinPos;
  return ans;
}
long long rlong() {
  readAhead(32);
  long long x = 0;
  bool neg = false;
  while (*stdinPos == ' ' || *stdinPos == '\n') ++stdinPos;
  if (*stdinPos == '-') {
    ++stdinPos;
    neg = true;
  }
  while (*stdinPos >= '0' && *stdinPos <= '9') {
    x *= 10;
    x += *stdinPos - '0';
    ++stdinPos;
  }
  return neg ? -x : x;
}

```