/*
ID: kfoozminus
TASK:
LANG: C++
*/

#include <bits/stdc++.h>
using namespace std;
//typedefs
/*
typedef long long ll;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <int, pii> piii;
typedef vector <int> vi;
typedef vector <ll> vl;
typedef pair <ll, ll> pll;
const double PI = acos(-1);
*/
//typedefs
#define ll long long
#define ull unsigned long long
#define pii pair <int, int>
#define piii pair <int, pii>
#define vi vector <int>
#define vl vector <ll>
#define pll pair <ll, ll>
#define PI acos(-1)
//defines
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define mem(a, b) memset(a, b, sizeof(a))
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define SQR(a) ((a)*(a))
#define inf 100000000
#define mod 1000000007
#define mod1 1000000007
#define mod2 1000000009
#define b1 43
#define b2 41
#define EPS 1e-9
//define harmonic(n) 0.57721566490153286l+log(n)
#define nl puts("")
#define odd(n) ((n)&1)
#define even(n) (!((n)&1))
#define vsort(v) sort(v.begin(), v.end())
#define lc (node<<1)
#define rc ((node<<1)|1)
#define ran(a, b) ((((rand() << 15) ^ rand()) % ((b) - (a) + 1)) + (a))
//loop
#define rep(i, n) for(int i = 0; i < n; ++i)
#define REP(i, n) for(int i = 1; i <= n; ++i)
//input
#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d%d", &a, &b)
#define siii(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define sl(a) scanf("%lld", &a)
#define sll(a, b) scanf("%lld%lld", &a, &b)
#define slll(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)
#define sd(a) scanf("%lf", &a)
#define sc(a) scanf("%c", &a)
#define sst(a) scanf("%s", a)
inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
//debug
#ifdef kfoozminus

template < typename F, typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
	return os << "(" << p.first << ", " << p.second << ")";
}

template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
	os << "{";
	for(auto it = v.begin(); it != v.end(); ++it) {
		if( it != v.begin() ) os << ", ";
		os << *it;
	}
    return os << "}";
}

template < typename T >
ostream &operator << ( ostream & os, const set< T > &v ) {
	os << "[";
	for(auto it = v.begin(); it != v.end(); ++it) {
		if( it != v.begin()) os << ", ";
		os << *it;
	}
    return os << "]";
}

template < typename F, typename S >
ostream &operator << ( ostream & os, const map< F, S > &v ) {
	os << "[";
	for(auto it = v.begin(); it != v.end(); ++it) {
		if( it != v.begin() ) os << ", ";
		os << it -> first << " = " << it -> second ;
	}
    return os << "]";
}

#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)

clock_t tStart = clock();
#define timeStamp dbg("Execution Time: ", (double)(clock() - tStart)/CLOCKS_PER_SEC)

void faltu () { cerr << endl; }

template <typename T>
void faltu( T a[], int n ) {
	for(int i = 0; i < n; ++i) cerr << a[i] << ' ';
	cerr << endl;
}

template <typename T, typename ... hello>
void faltu( T arg, const hello &... rest) { cerr << arg << ' '; faltu(rest...); }

#else
#define dbg(args...)
#endif
/*
ll add(ll a, ll b) {
	ll ret = a+b;
	if(ret >= mod) ret -= mod;
	return ret;
}

ll subtract(ll a, ll b) {
	ll ret = a-b;
	if(ret < 0) ret += mod;
	return ret;
}

ll mult(ll a, ll b) {
	ll ret = a*b;
	if(ret >= mod) ret %= mod;
	return ret;
}

ll bigmod(ll a, ll b) {
	ll ret = 1;
	while(b) {
		if(b&1) ret = mult(ret, a);
		b >>= 1; a = mult(a, a);
	}
	return ret;
}
*/

//ll inverse(ll n) { return bigmod(n, mod-2); }

/*
#define PMX 1000000

int marked[PMX/64 + 2];
vi primes;

#define mark(x) marked[x >> 6] |= (1 << ((x & 63) >> 1))
#define check(x) (marked[x >> 6] & (1 << ((x & 63) >> 1)))

bool isPrime(int x)
{
	return (x > 1) && ((x == 2) || ((x & 1) && (!(check(x)))));
}

void seive(int n)
{
	int i, j;
	for(i = 3; i * i <= n; i += 2) if(!check(i)) for(j = i * i; j <= n; j += i << 1) mark(j);
	primes.PB(2);
	for(i = 3; i <= n; i += 2) if(isPrime(i)) primes.PB(i);
}
*/

//Direction Array
//int fx[]={1, -1, 0, 0}; int fy[]={0, 0, 1, -1};
//int fx[]={0, 0, 1, -1, -1, 1, -1, 1}; int fy[]={-1, 1, 0, 0, 1, 1, -1, -1};

//bit manipulation
/*
bool checkBit(int n, int i) { return (n&(1<<i)); }
int setBit(int n, int i) { return (n|(1<<i)); }
int resetBit(int n, int i) { return (n&(~(1<<i))); }
*/
//end of template

#define MX 100007

set< pair< pll, int > > s1, s2, c1, c2;
//pair< pll, int > v;
set< pair< pii, pii > > st, c;
ll s[MX], vis[MX], a[MX], b[MX], SS;
int n;

ll f(ll tp1, ll tp2)
{
	int i;
	c = st;
	//c1 = s1;
	//c2 = s2;
	for(i = 0; i < n; i ++) vis[i] = s[i];

	ll ret = 0;
	ll tot = tp1 * SS;
	ll sum = 0;
	auto it = c.begin();
	while(sum < tot && it != c.end()) {
		pair< pii, pii> v = *it;
		ll ad = min(tot - sum, vis[v.S.S]);
		sum += ad;
		ret += ad * a[v.S.S];
		it ++;
		vis[v.S.S] -= ad;
	}

	/*ll ret = 0;
	ll tot = tp1 * SS;
	ll sum = 0;
	auto it = c1.begin();
	while(sum < tot && it != c1.end()) {
		v = *it;
		ll ad = min(tot - sum, vis[v.S]);
		sum += ad;
		ret += ad * a[v.S];
		it ++;
		vis[v.S] -= ad;
	}
	tot = tp2 * SS;
	sum = 0;
	it = c2.begin();
	while(sum < tot && it != c2.end()) {
		v = *it;
		ll ad = min(tot - sum, vis[v.S]);
		sum += ad;
		ret += ad * b[v.S];
		it ++;
		vis[v.S] -= ad;
	}*/
	for(i = 0; i < n; i ++) ret += vis[i] * b[i];
	return ret;
}

int main () {
#ifdef kfoozminus
//	freopen("in", "r", stdin);
//	freopen("out", "w", stdout);
#endif
	int i;
	ll tot, mn, mx, mid1, mid2, ans1, ans2, sum = 0, ans = 0;

	scanf("%d %lld", &n, &SS);
	for(i = 0; i < n; i ++) {
		scanf("%lld %lld %lld", &s[i], &a[i], &b[i]);
		st.insert( {{-(a[i] - b[i]), -a[i]}, {b[i], i}} );
		//s2.insert( {{-(b[i] - a[i]), -b[i]}, i} );
		sum += s[i];
	}
	tot = (sum + SS - 1)/SS;

	mn = 0;
	mx = tot;
	//for(i = 0; i <= tot; i ++) ans = max(ans, f(i, tot - i));
	while(mn < mx) {
		mid1 = mn + (mx - mn) /3;
		mid2 = mx - (mx - mn) /3;
		dbg(mid1, mid2);

		ans1 = f(mid1, tot - mid1);
		ans2 = f(mid2, tot - mid2);
		dbg(ans1, ans2);

		if(ans1 < ans2) mn = mid1 + 1;
		else mx = mid2 - 1;
	}
	ans = f(mn, tot - mn);
	cout << ans << endl;

//	timeStamp;
	return 0;
}


/*
ID: kfoozminus
TASK:
LANG: C++
*/

#include <bits/stdc++.h>
using namespace std;
//typedefs
/*
typedef long long ll;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <int, pii> piii;
typedef vector <int> vi;
typedef vector <ll> vl;
typedef pair <ll, ll> pll;
const double PI = acos(-1);
*/
//typedefs
#define ll long long
#define ull unsigned long long
#define pii pair <int, int>
#define piii pair <int, pii>
#define vi vector <int>
#define vl vector <ll>
#define pll pair <ll, ll>
#define PI acos(-1)
//defines
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define mem(a, b) memset(a, b, sizeof(a))
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define SQR(a) ((a)*(a))
#define inf 100000000
#define mod 1000000007
#define mod1 1000000007
#define mod2 1000000009
#define b1 43
#define b2 41
#define EPS 1e-9
//define harmonic(n) 0.57721566490153286l+log(n)
#define nl puts("")
#define odd(n) ((n)&1)
#define even(n) (!((n)&1))
#define vsort(v) sort(v.begin(), v.end())
#define lc (node<<1)
#define rc ((node<<1)|1)
#define ran(a, b) ((((rand() << 15) ^ rand()) % ((b) - (a) + 1)) + (a))
//loop
#define rep(i, n) for(int i = 0; i < n; ++i)
#define REP(i, n) for(int i = 1; i <= n; ++i)
//input
#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d%d", &a, &b)
#define siii(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define sl(a) scanf("%lld", &a)
#define sll(a, b) scanf("%lld%lld", &a, &b)
#define slll(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)
#define sd(a) scanf("%lf", &a)
#define sc(a) scanf("%c", &a)
#define sst(a) scanf("%s", a)
inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
//debug
#ifdef kfoozminus

template < typename F, typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
	return os << "(" << p.first << ", " << p.second << ")";
}

template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
	os << "{";
	for(auto it = v.begin(); it != v.end(); ++it) {
		if( it != v.begin() ) os << ", ";
		os << *it;
	}
    return os << "}";
}

template < typename T >
ostream &operator << ( ostream & os, const set< T > &v ) {
	os << "[";
	for(auto it = v.begin(); it != v.end(); ++it) {
		if( it != v.begin()) os << ", ";
		os << *it;
	}
    return os << "]";
}

template < typename F, typename S >
ostream &operator << ( ostream & os, const map< F, S > &v ) {
	os << "[";
	for(auto it = v.begin(); it != v.end(); ++it) {
		if( it != v.begin() ) os << ", ";
		os << it -> first << " = " << it -> second ;
	}
    return os << "]";
}

#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)

clock_t tStart = clock();
#define timeStamp dbg("Execution Time: ", (double)(clock() - tStart)/CLOCKS_PER_SEC)

void faltu () { cerr << endl; }

template <typename T>
void faltu( T a[], int n ) {
	for(int i = 0; i < n; ++i) cerr << a[i] << ' ';
	cerr << endl;
}

template <typename T, typename ... hello>
void faltu( T arg, const hello &... rest) { cerr << arg << ' '; faltu(rest...); }

#else
#define dbg(args...)
#endif
/*
ll add(ll a, ll b) {
	ll ret = a+b;
	if(ret >= mod) ret -= mod;
	return ret;
}

ll subtract(ll a, ll b) {
	ll ret = a-b;
	if(ret < 0) ret += mod;
	return ret;
}

ll mult(ll a, ll b) {
	ll ret = a*b;
	if(ret >= mod) ret %= mod;
	return ret;
}

ll bigmod(ll a, ll b) {
	ll ret = 1;
	while(b) {
		if(b&1) ret = mult(ret, a);
		b >>= 1; a = mult(a, a);
	}
	return ret;
}
*/

//ll inverse(ll n) { return bigmod(n, mod-2); }

/*
#define PMX 1000000

int marked[PMX/64 + 2];
vi primes;

#define mark(x) marked[x >> 6] |= (1 << ((x & 63) >> 1))
#define check(x) (marked[x >> 6] & (1 << ((x & 63) >> 1)))

bool isPrime(int x)
{
	return (x > 1) && ((x == 2) || ((x & 1) && (!(check(x)))));
}

void seive(int n)
{
	int i, j;
	for(i = 3; i * i <= n; i += 2) if(!check(i)) for(j = i * i; j <= n; j += i << 1) mark(j);
	primes.PB(2);
	for(i = 3; i <= n; i += 2) if(isPrime(i)) primes.PB(i);
}
*/

//Direction Array
//int fx[]={1, -1, 0, 0}; int fy[]={0, 0, 1, -1};
//int fx[]={0, 0, 1, -1, -1, 1, -1, 1}; int fy[]={-1, 1, 0, 0, 1, 1, -1, -1};

//bit manipulation
/*
bool checkBit(int n, int i) { return (n&(1<<i)); }
int setBit(int n, int i) { return (n|(1<<i)); }
int resetBit(int n, int i) { return (n&(~(1<<i))); }
*/
//end of template

#define MX 100007

set< pair< pll, int > > s1, s2, c1, c2;
//pair< pll, int > v;
set< pii > st, c;
ll s[MX], vis[MX], a[MX], b[MX], SS;
int n;

ll f(ll tp1)
{
	int i;
	c = st;
	//c1 = s1;
	//c2 = s2;
	for(i = 0; i < n; i ++) vis[i] = s[i];

	ll ret = 0;
	ll tot = tp1 * SS;
	ll sum = 0;
	auto it = c.begin();
	while(sum < tot && it != c.end()) {
		pii v = *it;
		ll ad = min(tot - sum, vis[v.S]);
		sum += ad;
		ret += ad * a[v.S];
		it ++;
		vis[v.S] -= ad;
	}

	/*ll ret = 0;
	ll tot = tp1 * SS;
	ll sum = 0;
	auto it = c1.begin();
	while(sum < tot && it != c1.end()) {
		v = *it;
		ll ad = min(tot - sum, vis[v.S]);
		sum += ad;
		ret += ad * a[v.S];
		it ++;
		vis[v.S] -= ad;
	}
	tot = tp2 * SS;
	sum = 0;
	it = c2.begin();
	while(sum < tot && it != c2.end()) {
		v = *it;
		ll ad = min(tot - sum, vis[v.S]);
		sum += ad;
		ret += ad * b[v.S];
		it ++;
		vis[v.S] -= ad;
	}*/
	for(i = 0; i < n; i ++) ret += vis[i] * b[i];
	return ret;
}

int main () {
#ifdef kfoozminus
//	freopen("in", "r", stdin);
//	freopen("out", "w", stdout);
#endif
	int i;
	ll tot, mn, mx, mid1, mid2, ans1, ans2, sum = 0, ans = 0;

	scanf("%d %lld", &n, &SS);
	for(i = 0; i < n; i ++) {
		scanf("%lld %lld %lld", &s[i], &a[i], &b[i]);
		st.insert( {-(a[i] - b[i]), i} );
		//s2.insert( {{-(b[i] - a[i]), -b[i]}, i} );
		sum += s[i];
	}
	tot = (sum + SS - 1)/SS;

	mn = 0;
	mx = tot;
	//for(i = 0; i <= tot; i ++) ans = max(ans, f(i, tot - i));
	while(mn < mx) {
		mid1 = mn + (mx - mn) /3;
		mid2 = mx - (mx - mn) /3;
		dbg(mid1, mid2);

		ans1 = f(mid1);
		ans2 = f(mid2);
		dbg(ans1, ans2);

		if(ans1 < ans2) mn = mid1 + 1;
		else mx = mid2 - 1;
	}
	for(i = max(0LL, mn - 10); i <= min(tot, mn + 10); i ++) ans = max(ans, f(i));
	cout << ans << endl;

//	timeStamp;
	return 0;
}


/*
ID: kfoozminus
TASK:
LANG: C++
*/

#include <bits/stdc++.h>
using namespace std;
//typedefs
/*
typedef long long ll;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <int, pii> piii;
typedef vector <int> vi;
typedef vector <ll> vl;
typedef pair <ll, ll> pll;
const double PI = acos(-1);
*/
//typedefs
#define ll long long
#define ull unsigned long long
#define pii pair <int, int>
#define piii pair <int, pii>
#define vi vector <int>
#define vl vector <ll>
#define pll pair <ll, ll>
#define PI acos(-1)
//defines
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define mem(a, b) memset(a, b, sizeof(a))
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define SQR(a) ((a)*(a))
#define inf 100000000
#define mod 1000000007
#define mod1 1000000007
#define mod2 1000000009
#define b1 43
#define b2 41
#define EPS 1e-9
//define harmonic(n) 0.57721566490153286l+log(n)
#define nl puts("")
#define odd(n) ((n)&1)
#define even(n) (!((n)&1))
#define vsort(v) sort(v.begin(), v.end())
#define lc (node<<1)
#define rc ((node<<1)|1)
#define ran(a, b) ((((rand() << 15) ^ rand()) % ((b) - (a) + 1)) + (a))
//loop
#define rep(i, n) for(int i = 0; i < n; ++i)
#define REP(i, n) for(int i = 1; i <= n; ++i)
//input
#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d%d", &a, &b)
#define siii(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define sl(a) scanf("%lld", &a)
#define sll(a, b) scanf("%lld%lld", &a, &b)
#define slll(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)
#define sd(a) scanf("%lf", &a)
#define sc(a) scanf("%c", &a)
#define sst(a) scanf("%s", a)
inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
//debug
#ifdef kfoozminus

template < typename F, typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
	return os << "(" << p.first << ", " << p.second << ")";
}

template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
	os << "{";
	for(auto it = v.begin(); it != v.end(); ++it) {
		if( it != v.begin() ) os << ", ";
		os << *it;
	}
    return os << "}";
}

template < typename T >
ostream &operator << ( ostream & os, const set< T > &v ) {
	os << "[";
	for(auto it = v.begin(); it != v.end(); ++it) {
		if( it != v.begin()) os << ", ";
		os << *it;
	}
    return os << "]";
}

template < typename F, typename S >
ostream &operator << ( ostream & os, const map< F, S > &v ) {
	os << "[";
	for(auto it = v.begin(); it != v.end(); ++it) {
		if( it != v.begin() ) os << ", ";
		os << it -> first << " = " << it -> second ;
	}
    return os << "]";
}

#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)

clock_t tStart = clock();
#define timeStamp dbg("Execution Time: ", (double)(clock() - tStart)/CLOCKS_PER_SEC)

void faltu () { cerr << endl; }

template <typename T>
void faltu( T a[], int n ) {
	for(int i = 0; i < n; ++i) cerr << a[i] << ' ';
	cerr << endl;
}

template <typename T, typename ... hello>
void faltu( T arg, const hello &... rest) { cerr << arg << ' '; faltu(rest...); }

#else
#define dbg(args...)
#endif
/*
ll add(ll a, ll b) {
	ll ret = a+b;
	if(ret >= mod) ret -= mod;
	return ret;
}

ll subtract(ll a, ll b) {
	ll ret = a-b;
	if(ret < 0) ret += mod;
	return ret;
}

ll mult(ll a, ll b) {
	ll ret = a*b;
	if(ret >= mod) ret %= mod;
	return ret;
}

ll bigmod(ll a, ll b) {
	ll ret = 1;
	while(b) {
		if(b&1) ret = mult(ret, a);
		b >>= 1; a = mult(a, a);
	}
	return ret;
}
*/

//ll inverse(ll n) { return bigmod(n, mod-2); }

/*
#define PMX 1000000

int marked[PMX/64 + 2];
vi primes;

#define mark(x) marked[x >> 6] |= (1 << ((x & 63) >> 1))
#define check(x) (marked[x >> 6] & (1 << ((x & 63) >> 1)))

bool isPrime(int x)
{
	return (x > 1) && ((x == 2) || ((x & 1) && (!(check(x)))));
}

void seive(int n)
{
	int i, j;
	for(i = 3; i * i <= n; i += 2) if(!check(i)) for(j = i * i; j <= n; j += i << 1) mark(j);
	primes.PB(2);
	for(i = 3; i <= n; i += 2) if(isPrime(i)) primes.PB(i);
}
*/

//Direction Array
//int fx[]={1, -1, 0, 0}; int fy[]={0, 0, 1, -1};
//int fx[]={0, 0, 1, -1, -1, 1, -1, 1}; int fy[]={-1, 1, 0, 0, 1, 1, -1, -1};

//bit manipulation
/*
bool checkBit(int n, int i) { return (n&(1<<i)); }
int setBit(int n, int i) { return (n|(1<<i)); }
int resetBit(int n, int i) { return (n&(~(1<<i))); }
*/
//end of template

#define MX 100007

set< pair< pll, int > > s1, s2, c1, c2;
//pair< pll, int > v;
set< pii > st, c;
ll s[MX], vis[MX], a[MX], b[MX], SS;
int n;
ll R;

ll f(ll tp1)
{
	int i;
	c = st;
	//c1 = s1;
	//c2 = s2;
	for(i = 0; i < n; i ++) vis[i] = s[i];

	ll ret = 0;
	ll tot = tp1 * SS;
	ll sum = 0;
	auto it = c.begin();
	if(tot) {
		tot -= R;
		while(sum < tot && it != c.end()) {
			pii v = *it;
			ll ad = min(tot - sum, vis[v.S]);
			sum += ad;
			ret += ad * a[v.S];
			it ++;
			vis[v.S] -= ad;
		}
		it --;
		tot += R;
	}
	for(i = 0; i < n; i ++) ret += vis[i] * b[i];
	ll ans = ret;
	while(sum < tot && it != c.end()) {
		pii v = *it;
		if(vis[v.S] == 0) break;
		sum ++;
		ret += a[v.S];
		ret -= b[v.S];
		vis[v.S] --;
		if(vis[v.S] == 0) it ++;
		ans = max(ans, ret);
	}

	/*ll ret = 0;
	ll tot = tp1 * SS;
	ll sum = 0;
	auto it = c1.begin();
	while(sum < tot && it != c1.end()) {
		v = *it;
		ll ad = min(tot - sum, vis[v.S]);
		sum += ad;
		ret += ad * a[v.S];
		it ++;
		vis[v.S] -= ad;
	}
	tot = tp2 * SS;
	sum = 0;
	it = c2.begin();
	while(sum < tot && it != c2.end()) {
		v = *it;
		ll ad = min(tot - sum, vis[v.S]);
		sum += ad;
		ret += ad * b[v.S];
		it ++;
		vis[v.S] -= ad;
	}*/
	return ans;
}

int main () {
#ifdef kfoozminus
//	freopen("in", "r", stdin);
//	freopen("out", "w", stdout);
#endif
	int i;
	ll tot, mn, mx, mid1, mid2, ans1, ans2, sum = 0, ans = 0;

	scanf("%d %lld", &n, &SS);
	for(i = 0; i < n; i ++) {
		scanf("%lld %lld %lld", &s[i], &a[i], &b[i]);
		st.insert( {-(a[i] - b[i]), i} );
		//s2.insert( {{-(b[i] - a[i]), -b[i]}, i} );
		sum += s[i];
	}
	tot = (sum + SS - 1)/SS;
	R = tot * SS - sum;

	mn = 0;
	mx = tot;
	//for(i = 0; i <= tot; i ++) ans = max(ans, f(i, tot - i));
	while(mn < mx) {
		mid1 = mn + (mx - mn) /3;
		mid2 = mx - (mx - mn) /3;
		dbg(mid1, mid2);

		ans1 = f(mid1);
		ans2 = f(mid2);
		dbg(ans1, ans2);

		if(ans1 < ans2) mn = mid1 + 1;
		else mx = mid2 - 1;
	}
	cout << f(mn) << endl;

//	timeStamp;
	return 0;
}


/*
ID: kfoozminus
TASK:
LANG: C++
*/

#include <bits/stdc++.h>
using namespace std;
//typedefs
/*
typedef long long ll;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <int, pii> piii;
typedef vector <int> vi;
typedef vector <ll> vl;
typedef pair <ll, ll> pll;
const double PI = acos(-1);
*/
//typedefs
#define ll long long
#define ull unsigned long long
#define pii pair <int, int>
#define piii pair <int, pii>
#define vi vector <int>
#define vl vector <ll>
#define pll pair <ll, ll>
#define PI acos(-1)
//defines
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define mem(a, b) memset(a, b, sizeof(a))
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define SQR(a) ((a)*(a))
#define inf 100000000
#define mod 1000000007
#define mod1 1000000007
#define mod2 1000000009
#define b1 43
#define b2 41
#define EPS 1e-9
//define harmonic(n) 0.57721566490153286l+log(n)
#define nl puts("")
#define odd(n) ((n)&1)
#define even(n) (!((n)&1))
#define vsort(v) sort(v.begin(), v.end())
#define lc (node<<1)
#define rc ((node<<1)|1)
#define ran(a, b) ((((rand() << 15) ^ rand()) % ((b) - (a) + 1)) + (a))
//loop
#define rep(i, n) for(int i = 0; i < n; ++i)
#define REP(i, n) for(int i = 1; i <= n; ++i)
//input
#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d%d", &a, &b)
#define siii(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define sl(a) scanf("%lld", &a)
#define sll(a, b) scanf("%lld%lld", &a, &b)
#define slll(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)
#define sd(a) scanf("%lf", &a)
#define sc(a) scanf("%c", &a)
#define sst(a) scanf("%s", a)
inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
//debug
#ifdef kfoozminus

template < typename F, typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
	return os << "(" << p.first << ", " << p.second << ")";
}

template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
	os << "{";
	for(auto it = v.begin(); it != v.end(); ++it) {
		if( it != v.begin() ) os << ", ";
		os << *it;
	}
    return os << "}";
}

template < typename T >
ostream &operator << ( ostream & os, const set< T > &v ) {
	os << "[";
	for(auto it = v.begin(); it != v.end(); ++it) {
		if( it != v.begin()) os << ", ";
		os << *it;
	}
    return os << "]";
}

template < typename F, typename S >
ostream &operator << ( ostream & os, const map< F, S > &v ) {
	os << "[";
	for(auto it = v.begin(); it != v.end(); ++it) {
		if( it != v.begin() ) os << ", ";
		os << it -> first << " = " << it -> second ;
	}
    return os << "]";
}

#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)

clock_t tStart = clock();
#define timeStamp dbg("Execution Time: ", (double)(clock() - tStart)/CLOCKS_PER_SEC)

void faltu () { cerr << endl; }

template <typename T>
void faltu( T a[], int n ) {
	for(int i = 0; i < n; ++i) cerr << a[i] << ' ';
	cerr << endl;
}

template <typename T, typename ... hello>
void faltu( T arg, const hello &... rest) { cerr << arg << ' '; faltu(rest...); }

#else
#define dbg(args...)
#endif
/*
ll add(ll a, ll b) {
	ll ret = a+b;
	if(ret >= mod) ret -= mod;
	return ret;
}

ll subtract(ll a, ll b) {
	ll ret = a-b;
	if(ret < 0) ret += mod;
	return ret;
}

ll mult(ll a, ll b) {
	ll ret = a*b;
	if(ret >= mod) ret %= mod;
	return ret;
}

ll bigmod(ll a, ll b) {
	ll ret = 1;
	while(b) {
		if(b&1) ret = mult(ret, a);
		b >>= 1; a = mult(a, a);
	}
	return ret;
}
*/

//ll inverse(ll n) { return bigmod(n, mod-2); }

/*
#define PMX 1000000

int marked[PMX/64 + 2];
vi primes;

#define mark(x) marked[x >> 6] |= (1 << ((x & 63) >> 1))
#define check(x) (marked[x >> 6] & (1 << ((x & 63) >> 1)))

bool isPrime(int x)
{
	return (x > 1) && ((x == 2) || ((x & 1) && (!(check(x)))));
}

void seive(int n)
{
	int i, j;
	for(i = 3; i * i <= n; i += 2) if(!check(i)) for(j = i * i; j <= n; j += i << 1) mark(j);
	primes.PB(2);
	for(i = 3; i <= n; i += 2) if(isPrime(i)) primes.PB(i);
}
*/

//Direction Array
//int fx[]={1, -1, 0, 0}; int fy[]={0, 0, 1, -1};
//int fx[]={0, 0, 1, -1, -1, 1, -1, 1}; int fy[]={-1, 1, 0, 0, 1, 1, -1, -1};

//bit manipulation
/*
bool checkBit(int n, int i) { return (n&(1<<i)); }
int setBit(int n, int i) { return (n|(1<<i)); }
int resetBit(int n, int i) { return (n&(~(1<<i))); }
*/
//end of template

#define MX 100007

set< pair< pll, int > > s1, s2, c1, c2;
//pair< pll, int > v;
set< pii > st, c;
ll s[MX], vis[MX], a[MX], b[MX], SS;
int n;
ll R;

ll f(ll tp1)
{
	int i;
	c = st;
	//c1 = s1;
	//c2 = s2;
	for(i = 0; i < n; i ++) vis[i] = s[i];

	ll ret = 0;
	ll tot = tp1 * SS;
	ll sum = 0;
	auto it = c.begin();
	if(tot) {
		tot -= R;
		while(sum < tot && it != c.end()) {
			pii v = *it;
			ll ad = min(tot - sum, vis[v.S]);
			sum += ad;
			ret += ad * a[v.S];
			vis[v.S] -= ad;
			if(vis[v.S] == 0) it ++;
		}
		tot += R;
	}
	for(i = 0; i < n; i ++) ret += vis[i] * b[i];
	ll ans = ret;
	while(sum < tot && it != c.end()) {
		pii v = *it;
		sum ++;
		ret += a[v.S];
		ret -= b[v.S];
		vis[v.S] --;
		if(vis[v.S] == 0) it ++;
		ans = max(ans, ret);
	}

	/*ll ret = 0;
	ll tot = tp1 * SS;
	ll sum = 0;
	auto it = c1.begin();
	while(sum < tot && it != c1.end()) {
		v = *it;
		ll ad = min(tot - sum, vis[v.S]);
		sum += ad;
		ret += ad * a[v.S];
		it ++;
		vis[v.S] -= ad;
	}
	tot = tp2 * SS;
	sum = 0;
	it = c2.begin();
	while(sum < tot && it != c2.end()) {
		v = *it;
		ll ad = min(tot - sum, vis[v.S]);
		sum += ad;
		ret += ad * b[v.S];
		it ++;
		vis[v.S] -= ad;
	}*/
	return ans;
}

int main () {
#ifdef kfoozminus
//	freopen("in", "r", stdin);
//	freopen("out", "w", stdout);
#endif
	int i;
	ll tot, mn, mx, mid1, mid2, ans1, ans2, sum = 0, ans = 0;

	scanf("%d %lld", &n, &SS);
	for(i = 0; i < n; i ++) {
		scanf("%lld %lld %lld", &s[i], &a[i], &b[i]);
		st.insert( {-(a[i] - b[i]), i} );
		//s2.insert( {{-(b[i] - a[i]), -b[i]}, i} );
		sum += s[i];
	}
	tot = (sum + SS - 1)/SS;
	R = tot * SS - sum;

	mn = 0;
	mx = tot;
	//for(i = 0; i <= tot; i ++) ans = max(ans, f(i, tot - i));
	while(mn < mx) {
		mid1 = mn + (mx - mn) /3;
		mid2 = mx - (mx - mn) /3;
		dbg(mid1, mid2);

		ans1 = f(mid1);
		ans2 = f(mid2);
		dbg(ans1, ans2);

		if(ans1 < ans2) mn = mid1 + 1;
		else mx = mid2 - 1;
	}
	cout << f(mn) << endl;

//	timeStamp;
	return 0;
}


/*
ID: kfoozminus
TASK:
LANG: C++
*/

#include <bits/stdc++.h>
using namespace std;
//typedefs
/*
typedef long long ll;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <int, pii> piii;
typedef vector <int> vi;
typedef vector <ll> vl;
typedef pair <ll, ll> pll;
const double PI = acos(-1);
*/
//typedefs
#define ll long long
#define ull unsigned long long
#define pii pair <int, int>
#define piii pair <int, pii>
#define vi vector <int>
#define vl vector <ll>
#define pll pair <ll, ll>
#define PI acos(-1)
//defines
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define mem(a, b) memset(a, b, sizeof(a))
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define SQR(a) ((a)*(a))
#define inf 100000000
#define mod 1000000007
#define mod1 1000000007
#define mod2 1000000009
#define b1 43
#define b2 41
#define EPS 1e-9
//define harmonic(n) 0.57721566490153286l+log(n)
#define nl puts("")
#define odd(n) ((n)&1)
#define even(n) (!((n)&1))
#define vsort(v) sort(v.begin(), v.end())
#define lc (node<<1)
#define rc ((node<<1)|1)
#define ran(a, b) ((((rand() << 15) ^ rand()) % ((b) - (a) + 1)) + (a))
//loop
#define rep(i, n) for(int i = 0; i < n; ++i)
#define REP(i, n) for(int i = 1; i <= n; ++i)
//input
#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d%d", &a, &b)
#define siii(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define sl(a) scanf("%lld", &a)
#define sll(a, b) scanf("%lld%lld", &a, &b)
#define slll(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)
#define sd(a) scanf("%lf", &a)
#define sc(a) scanf("%c", &a)
#define sst(a) scanf("%s", a)
inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
//debug
#ifdef kfoozminus

template < typename F, typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
	return os << "(" << p.first << ", " << p.second << ")";
}

template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
	os << "{";
	for(auto it = v.begin(); it != v.end(); ++it) {
		if( it != v.begin() ) os << ", ";
		os << *it;
	}
    return os << "}";
}

template < typename T >
ostream &operator << ( ostream & os, const set< T > &v ) {
	os << "[";
	for(auto it = v.begin(); it != v.end(); ++it) {
		if( it != v.begin()) os << ", ";
		os << *it;
	}
    return os << "]";
}

template < typename F, typename S >
ostream &operator << ( ostream & os, const map< F, S > &v ) {
	os << "[";
	for(auto it = v.begin(); it != v.end(); ++it) {
		if( it != v.begin() ) os << ", ";
		os << it -> first << " = " << it -> second ;
	}
    return os << "]";
}

#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)

clock_t tStart = clock();
#define timeStamp dbg("Execution Time: ", (double)(clock() - tStart)/CLOCKS_PER_SEC)

void faltu () { cerr << endl; }

template <typename T>
void faltu( T a[], int n ) {
	for(int i = 0; i < n; ++i) cerr << a[i] << ' ';
	cerr << endl;
}

template <typename T, typename ... hello>
void faltu( T arg, const hello &... rest) { cerr << arg << ' '; faltu(rest...); }

#else
#define dbg(args...)
#endif
/*
ll add(ll a, ll b) {
	ll ret = a+b;
	if(ret >= mod) ret -= mod;
	return ret;
}

ll subtract(ll a, ll b) {
	ll ret = a-b;
	if(ret < 0) ret += mod;
	return ret;
}

ll mult(ll a, ll b) {
	ll ret = a*b;
	if(ret >= mod) ret %= mod;
	return ret;
}

ll bigmod(ll a, ll b) {
	ll ret = 1;
	while(b) {
		if(b&1) ret = mult(ret, a);
		b >>= 1; a = mult(a, a);
	}
	return ret;
}
*/

//ll inverse(ll n) { return bigmod(n, mod-2); }

/*
#define PMX 1000000

int marked[PMX/64 + 2];
vi primes;

#define mark(x) marked[x >> 6] |= (1 << ((x & 63) >> 1))
#define check(x) (marked[x >> 6] & (1 << ((x & 63) >> 1)))

bool isPrime(int x)
{
	return (x > 1) && ((x == 2) || ((x & 1) && (!(check(x)))));
}

void seive(int n)
{
	int i, j;
	for(i = 3; i * i <= n; i += 2) if(!check(i)) for(j = i * i; j <= n; j += i << 1) mark(j);
	primes.PB(2);
	for(i = 3; i <= n; i += 2) if(isPrime(i)) primes.PB(i);
}
*/

//Direction Array
//int fx[]={1, -1, 0, 0}; int fy[]={0, 0, 1, -1};
//int fx[]={0, 0, 1, -1, -1, 1, -1, 1}; int fy[]={-1, 1, 0, 0, 1, 1, -1, -1};

//bit manipulation
/*
bool checkBit(int n, int i) { return (n&(1<<i)); }
int setBit(int n, int i) { return (n|(1<<i)); }
int resetBit(int n, int i) { return (n&(~(1<<i))); }
*/
//end of template

#define MX 100007

set< pair< pll, int > > s1, s2, c1, c2;
//pair< pll, int > v;
set< pii > st, c;
ll s[MX], vis[MX], a[MX], b[MX], SS;
int n;
ll R;

ll f(ll tp1)
{
	int i;
	c = st;
	for(i = 0; i < n; i ++) vis[i] = s[i];

	ll ret = 0;
	ll tot = tp1 * SS;
	ll sum = 0;
	auto it = c.begin();
	if(tot) {
		tot -= R;
		while(sum < tot && it != c.end()) {
			pii v = *it;
			ll ad = min(tot - sum, vis[v.S]);
			sum += ad;
			ret += ad * a[v.S];
			vis[v.S] -= ad;
			if(vis[v.S] == 0) it ++;
		}
		tot += R;
	}
	for(i = 0; i < n; i ++) ret += vis[i] * b[i];
	ll ans = ret;
	while(sum < tot && it != c.end()) {
		pii v = *it;
		sum ++;
		ret += a[v.S];
		ret -= b[v.S];
		vis[v.S] --;
		if(vis[v.S] == 0) it ++;
		ans = max(ans, ret);
	}
	return ans;
}

int main () {
#ifdef kfoozminus
//	freopen("in", "r", stdin);
//	freopen("out", "w", stdout);
#endif
	int i;
	ll tot, mn, mx, mid1, mid2, ans1, ans2, sum = 0, ans = 0;

	scanf("%d %lld", &n, &SS);
	for(i = 0; i < n; i ++) {
		scanf("%lld %lld %lld", &s[i], &a[i], &b[i]);
		st.insert( {-(a[i] - b[i]), i} );
		//s2.insert( {{-(b[i] - a[i]), -b[i]}, i} );
		sum += s[i];
	}
	tot = (sum + SS - 1)/SS;
	R = tot * SS - sum;

	mn = 0;
	mx = tot;
	while(mn < mx) {
		mid1 = mn + (mx - mn) /3;
		mid2 = mx - (mx - mn) /3;
		dbg(mid1, mid2);

		ans1 = f(mid1);
		ans2 = f(mid2);
		dbg(ans1, ans2);

		if(ans1 < ans2) mn = mid1 + 1;
		else mx = mid2 - 1;
	}
	cout << f(mn) << endl;

//	timeStamp;
	return 0;
}


/*
ID: kfoozminus
TASK:
LANG: C++
*/

#include <bits/stdc++.h>
using namespace std;
//typedefs
/*
typedef long long ll;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <int, pii> piii;
typedef vector <int> vi;
typedef vector <ll> vl;
typedef pair <ll, ll> pll;
const double PI = acos(-1);
*/
//typedefs
#define ll long long
#define ull unsigned long long
#define pii pair <int, int>
#define piii pair <int, pii>
#define vi vector <int>
#define vl vector <ll>
#define pll pair <ll, ll>
#define PI acos(-1)
//defines
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define mem(a, b) memset(a, b, sizeof(a))
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define SQR(a) ((a)*(a))
#define inf 100000000
#define mod 1000000007
#define mod1 1000000007
#define mod2 1000000009
#define b1 43
#define b2 41
#define EPS 1e-9
//define harmonic(n) 0.57721566490153286l+log(n)
#define nl puts("")
#define odd(n) ((n)&1)
#define even(n) (!((n)&1))
#define vsort(v) sort(v.begin(), v.end())
#define lc (node<<1)
#define rc ((node<<1)|1)
#define ran(a, b) ((((rand() << 15) ^ rand()) % ((b) - (a) + 1)) + (a))
//loop
#define rep(i, n) for(int i = 0; i < n; ++i)
#define REP(i, n) for(int i = 1; i <= n; ++i)
//input
#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d%d", &a, &b)
#define siii(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define sl(a) scanf("%lld", &a)
#define sll(a, b) scanf("%lld%lld", &a, &b)
#define slll(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)
#define sd(a) scanf("%lf", &a)
#define sc(a) scanf("%c", &a)
#define sst(a) scanf("%s", a)
inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
//debug
#ifdef kfoozminus

template < typename F, typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
	return os << "(" << p.first << ", " << p.second << ")";
}

template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
	os << "{";
	for(auto it = v.begin(); it != v.end(); ++it) {
		if( it != v.begin() ) os << ", ";
		os << *it;
	}
    return os << "}";
}

template < typename T >
ostream &operator << ( ostream & os, const set< T > &v ) {
	os << "[";
	for(auto it = v.begin(); it != v.end(); ++it) {
		if( it != v.begin()) os << ", ";
		os << *it;
	}
    return os << "]";
}

template < typename F, typename S >
ostream &operator << ( ostream & os, const map< F, S > &v ) {
	os << "[";
	for(auto it = v.begin(); it != v.end(); ++it) {
		if( it != v.begin() ) os << ", ";
		os << it -> first << " = " << it -> second ;
	}
    return os << "]";
}

#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)

clock_t tStart = clock();
#define timeStamp dbg("Execution Time: ", (double)(clock() - tStart)/CLOCKS_PER_SEC)

void faltu () { cerr << endl; }

template <typename T>
void faltu( T a[], int n ) {
	for(int i = 0; i < n; ++i) cerr << a[i] << ' ';
	cerr << endl;
}

template <typename T, typename ... hello>
void faltu( T arg, const hello &... rest) { cerr << arg << ' '; faltu(rest...); }

#else
#define dbg(args...)
#endif
/*
ll add(ll a, ll b) {
	ll ret = a+b;
	if(ret >= mod) ret -= mod;
	return ret;
}

ll subtract(ll a, ll b) {
	ll ret = a-b;
	if(ret < 0) ret += mod;
	return ret;
}

ll mult(ll a, ll b) {
	ll ret = a*b;
	if(ret >= mod) ret %= mod;
	return ret;
}

ll bigmod(ll a, ll b) {
	ll ret = 1;
	while(b) {
		if(b&1) ret = mult(ret, a);
		b >>= 1; a = mult(a, a);
	}
	return ret;
}
*/

//ll inverse(ll n) { return bigmod(n, mod-2); }

/*
#define PMX 1000000

int marked[PMX/64 + 2];
vi primes;

#define mark(x) marked[x >> 6] |= (1 << ((x & 63) >> 1))
#define check(x) (marked[x >> 6] & (1 << ((x & 63) >> 1)))

bool isPrime(int x)
{
	return (x > 1) && ((x == 2) || ((x & 1) && (!(check(x)))));
}

void seive(int n)
{
	int i, j;
	for(i = 3; i * i <= n; i += 2) if(!check(i)) for(j = i * i; j <= n; j += i << 1) mark(j);
	primes.PB(2);
	for(i = 3; i <= n; i += 2) if(isPrime(i)) primes.PB(i);
}
*/

//Direction Array
//int fx[]={1, -1, 0, 0}; int fy[]={0, 0, 1, -1};
//int fx[]={0, 0, 1, -1, -1, 1, -1, 1}; int fy[]={-1, 1, 0, 0, 1, 1, -1, -1};

//bit manipulation
/*
bool checkBit(int n, int i) { return (n&(1<<i)); }
int setBit(int n, int i) { return (n|(1<<i)); }
int resetBit(int n, int i) { return (n&(~(1<<i))); }
*/
//end of template

#define MX 100007

set< pair< pll, int > > s1, s2, c1, c2;
//pair< pll, int > v;
set< pii > st, c;
ll s[MX], vis[MX], a[MX], b[MX], SS;
int n;
ll R;

ll f(ll tp1)
{
	int i;
	c = st;
	//c1 = s1;
	//c2 = s2;
	for(i = 0; i < n; i ++) vis[i] = s[i];

	ll ret = 0;
	ll tot = tp1 * SS;
	ll sum = 0;
	auto it = c.begin();
	if(tot) {
		tot -= R;
		while(sum < tot && it != c.end()) {
			pii v = *it;
			ll ad = min(tot - sum, vis[v.S]);
			sum += ad;
			ret += ad * a[v.S];
			it ++;
			vis[v.S] -= ad;
		}
		it --;
		tot += R;
	}
	for(i = 0; i < n; i ++) ret += vis[i] * b[i];
	ll ans = ret;
	while(sum < tot && it != c.end()) {
		pii v = *it;
		sum ++;
		ret += a[v.S];
		ret -= b[v.S];
		vis[v.S] --;
		if(vis[v.S] == 0) it ++;
		ans = max(ans, ret);
	}

	/*ll ret = 0;
	ll tot = tp1 * SS;
	ll sum = 0;
	auto it = c1.begin();
	while(sum < tot && it != c1.end()) {
		v = *it;
		ll ad = min(tot - sum, vis[v.S]);
		sum += ad;
		ret += ad * a[v.S];
		it ++;
		vis[v.S] -= ad;
	}
	tot = tp2 * SS;
	sum = 0;
	it = c2.begin();
	while(sum < tot && it != c2.end()) {
		v = *it;
		ll ad = min(tot - sum, vis[v.S]);
		sum += ad;
		ret += ad * b[v.S];
		it ++;
		vis[v.S] -= ad;
	}*/
	return ans;
}

int main () {
#ifdef kfoozminus
//	freopen("in", "r", stdin);
//	freopen("out", "w", stdout);
#endif
	int i;
	ll tot, mn, mx, mid1, mid2, ans1, ans2, sum = 0, ans = 0;

	scanf("%d %lld", &n, &SS);
	for(i = 0; i < n; i ++) {
		scanf("%lld %lld %lld", &s[i], &a[i], &b[i]);
		st.insert( {-(a[i] - b[i]), i} );
		//s2.insert( {{-(b[i] - a[i]), -b[i]}, i} );
		sum += s[i];
	}
	tot = (sum + SS - 1)/SS;
	R = tot * SS - sum;

	mn = 0;
	mx = tot;
	//for(i = 0; i <= tot; i ++) ans = max(ans, f(i, tot - i));
	while(mn < mx) {
		mid1 = mn + (mx - mn) /3;
		mid2 = mx - (mx - mn) /3;
		dbg(mid1, mid2);

		ans1 = f(mid1);
		ans2 = f(mid2);
		dbg(ans1, ans2);

		if(ans1 < ans2) mn = mid1 + 1;
		else mx = mid2 - 1;
	}
	cout << f(mn) << endl;

//	timeStamp;
	return 0;
}


/*
ID: kfoozminus
TASK:
LANG: C++
*/

#include <bits/stdc++.h>
using namespace std;
//typedefs
/*
typedef long long ll;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <int, pii> piii;
typedef vector <int> vi;
typedef vector <ll> vl;
typedef pair <ll, ll> pll;
const double PI = acos(-1);
*/
//typedefs
#define ll long long
#define ull unsigned long long
#define pii pair <int, int>
#define piii pair <int, pii>
#define vi vector <int>
#define vl vector <ll>
#define pll pair <ll, ll>
#define PI acos(-1)
//defines
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define mem(a, b) memset(a, b, sizeof(a))
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define SQR(a) ((a)*(a))
#define inf 100000000
#define mod 1000000007
#define mod1 1000000007
#define mod2 1000000009
#define b1 43
#define b2 41
#define EPS 1e-9
//define harmonic(n) 0.57721566490153286l+log(n)
#define nl puts("")
#define odd(n) ((n)&1)
#define even(n) (!((n)&1))
#define vsort(v) sort(v.begin(), v.end())
#define lc (node<<1)
#define rc ((node<<1)|1)
#define ran(a, b) ((((rand() << 15) ^ rand()) % ((b) - (a) + 1)) + (a))
//loop
#define rep(i, n) for(int i = 0; i < n; ++i)
#define REP(i, n) for(int i = 1; i <= n; ++i)
//input
#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d%d", &a, &b)
#define siii(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define sl(a) scanf("%lld", &a)
#define sll(a, b) scanf("%lld%lld", &a, &b)
#define slll(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)
#define sd(a) scanf("%lf", &a)
#define sc(a) scanf("%c", &a)
#define sst(a) scanf("%s", a)
inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
//debug
#ifdef kfoozminus

template < typename F, typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
	return os << "(" << p.first << ", " << p.second << ")";
}

template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
	os << "{";
	for(auto it = v.begin(); it != v.end(); ++it) {
		if( it != v.begin() ) os << ", ";
		os << *it;
	}
    return os << "}";
}

template < typename T >
ostream &operator << ( ostream & os, const set< T > &v ) {
	os << "[";
	for(auto it = v.begin(); it != v.end(); ++it) {
		if( it != v.begin()) os << ", ";
		os << *it;
	}
    return os << "]";
}

template < typename F, typename S >
ostream &operator << ( ostream & os, const map< F, S > &v ) {
	os << "[";
	for(auto it = v.begin(); it != v.end(); ++it) {
		if( it != v.begin() ) os << ", ";
		os << it -> first << " = " << it -> second ;
	}
    return os << "]";
}

#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)

clock_t tStart = clock();
#define timeStamp dbg("Execution Time: ", (double)(clock() - tStart)/CLOCKS_PER_SEC)

void faltu () { cerr << endl; }

template <typename T>
void faltu( T a[], int n ) {
	for(int i = 0; i < n; ++i) cerr << a[i] << ' ';
	cerr << endl;
}

template <typename T, typename ... hello>
void faltu( T arg, const hello &... rest) { cerr << arg << ' '; faltu(rest...); }

#else
#define dbg(args...)
#endif
/*
ll add(ll a, ll b) {
	ll ret = a+b;
	if(ret >= mod) ret -= mod;
	return ret;
}

ll subtract(ll a, ll b) {
	ll ret = a-b;
	if(ret < 0) ret += mod;
	return ret;
}

ll mult(ll a, ll b) {
	ll ret = a*b;
	if(ret >= mod) ret %= mod;
	return ret;
}

ll bigmod(ll a, ll b) {
	ll ret = 1;
	while(b) {
		if(b&1) ret = mult(ret, a);
		b >>= 1; a = mult(a, a);
	}
	return ret;
}
*/

//ll inverse(ll n) { return bigmod(n, mod-2); }

/*
#define PMX 1000000

int marked[PMX/64 + 2];
vi primes;

#define mark(x) marked[x >> 6] |= (1 << ((x & 63) >> 1))
#define check(x) (marked[x >> 6] & (1 << ((x & 63) >> 1)))

bool isPrime(int x)
{
	return (x > 1) && ((x == 2) || ((x & 1) && (!(check(x)))));
}

void seive(int n)
{
	int i, j;
	for(i = 3; i * i <= n; i += 2) if(!check(i)) for(j = i * i; j <= n; j += i << 1) mark(j);
	primes.PB(2);
	for(i = 3; i <= n; i += 2) if(isPrime(i)) primes.PB(i);
}
*/

//Direction Array
//int fx[]={1, -1, 0, 0}; int fy[]={0, 0, 1, -1};
//int fx[]={0, 0, 1, -1, -1, 1, -1, 1}; int fy[]={-1, 1, 0, 0, 1, 1, -1, -1};

//bit manipulation
/*
bool checkBit(int n, int i) { return (n&(1<<i)); }
int setBit(int n, int i) { return (n|(1<<i)); }
int resetBit(int n, int i) { return (n&(~(1<<i))); }
*/
//end of template

#define MX 100007

set< pair< pll, int > > s1, s2, c1, c2;
//pair< pll, int > v;
set< pii > st, c;
ll s[MX], vis[MX], a[MX], b[MX], SS;
int n;

ll f(ll tp1, ll tp2)
{
	int i;
	c = st;
	//c1 = s1;
	//c2 = s2;
	for(i = 0; i < n; i ++) vis[i] = s[i];

	ll ret = 0;
	ll tot = tp1 * SS;
	ll sum = 0;
	auto it = c.begin();
	while(sum < tot && it != c.end()) {
		pii v = *it;
		ll ad = min(tot - sum, vis[v.S]);
		sum += ad;
		ret += ad * a[v.S];
		it ++;
		vis[v.S] -= ad;
	}

	/*ll ret = 0;
	ll tot = tp1 * SS;
	ll sum = 0;
	auto it = c1.begin();
	while(sum < tot && it != c1.end()) {
		v = *it;
		ll ad = min(tot - sum, vis[v.S]);
		sum += ad;
		ret += ad * a[v.S];
		it ++;
		vis[v.S] -= ad;
	}
	tot = tp2 * SS;
	sum = 0;
	it = c2.begin();
	while(sum < tot && it != c2.end()) {
		v = *it;
		ll ad = min(tot - sum, vis[v.S]);
		sum += ad;
		ret += ad * b[v.S];
		it ++;
		vis[v.S] -= ad;
	}*/
	for(i = 0; i < n; i ++) ret += vis[i] * b[i];
	return ret;
}

int main () {
#ifdef kfoozminus
//	freopen("in", "r", stdin);
//	freopen("out", "w", stdout);
#endif
	int i;
	ll tot, mn, mx, mid1, mid2, ans1, ans2, sum = 0, ans = 0;

	scanf("%d %lld", &n, &SS);
	for(i = 0; i < n; i ++) {
		scanf("%lld %lld %lld", &s[i], &a[i], &b[i]);
		st.insert( {-(a[i] - b[i]), i} );
		//s2.insert( {{-(b[i] - a[i]), -b[i]}, i} );
		sum += s[i];
	}
	tot = (sum + SS - 1)/SS;

	mn = 0;
	mx = tot;
	//for(i = 0; i <= tot; i ++) ans = max(ans, f(i, tot - i));
	while(mn < mx) {
		mid1 = mn + (mx - mn) /3;
		mid2 = mx - (mx - mn) /3;
		dbg(mid1, mid2);

		ans1 = f(mid1, tot - mid1);
		ans2 = f(mid2, tot - mid2);
		dbg(ans1, ans2);

		if(ans1 < ans2) mn = mid1 + 1;
		else mx = mid2 - 1;
	}
	ans = f(mn, tot - mn);
	cout << ans << endl;

//	timeStamp;
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;

#define N 100007
typedef long long ll;

struct node
{
    ll s, a, b;
    node() {}
    node(ll s_, ll a_, ll b_)
    : s(s_), a(a_), b(b_) {}
};

int n; ll s0;
node A[N];
vector<node> V1, V2;

bool comp1(const node &x, const node &y)
{
    return (x.a - x.b) < (y.a - y.b);
}

bool comp2(const node &x, const node &y)
{
    return (x.b - x.a) < (y.b - y.a);
}

int main()
{
    scanf("%d%lld", &n, &s0);
    for (int i = 1; i <= n; i++)
    {
        ll s, a, b;
        scanf("%lld%lld%lld", &s, &a, &b);
        A[i] = node(s, a, b);
    }

    ll s1 = 0, s2 = 0;
    ll sum1 = 0, sum2 = 0;
    for (int i = 1; i <= n; i++)
        if (A[i].a > A[i].b)
        {
            s1 += A[i].s;
            sum1 += A[i].s * A[i].a;
            V1.push_back(A[i]);
        }
        else
        {
            s2 += A[i].s;
            sum2 += A[i].s * A[i].b;
            V2.push_back(A[i]);
        }

    ll r1 = s1 % s0, r2 = s2 % s0;

    if (r1 + r2 > s0)
    {
        ll r11 = s0 - r2;
        ll r22 = s0 - r1;
        r1 = r11, r2 = r22;
    }

    sort(V1.begin(), V1.end(), comp1);
    int len1 = V1.length(); ll t1 = 0, d1 = 0;
    for (int i = 0; i < len1 && t1 < r1; i++)
    {
        ll t = min(r1 - t1, V1[i].s);
        t1 += t;
        d1 += (V1[i].a - V1[i].b) * t;
    }

    sort(V2.begin(), V2.end(), comp2);
    int len2 = V2.length(); ll t2 = 0, d2 = 0;
    for (int i = 0; i < len2 && t2 < r2; i++)
    {
        ll t = min(r2 - t2, V2[i].s);
        t2 += t;
        d2 += (V2[i].b - V2[i].a) * t;
    }

    ll ans = sum1 + sum2;
    ans = max(ans - d1, ans - d2);
    printf("%lld\n", ans);
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;

#define N 100007
typedef long long ll;

struct node
{
    ll s, a, b;
    node() {}
    node(ll s_, ll a_, ll b_)
    : s(s_), a(a_), b(b_) {}
};

int n; ll s0;
node A[N];
vector<node> V1, V2;

bool comp1(const node &x, const node &y)
{
    return (x.a - x.b) < (y.a - y.b);
}

bool comp2(const node &x, const node &y)
{
    return (x.b - x.a) < (y.b - y.a);
}

int main()
{
    scanf("%d%lld", &n, &s0);
    for (int i = 1; i <= n; i++)
    {
        ll s, a, b;
        scanf("%lld%lld%lld", &s, &a, &b);
        A[i] = node(s, a, b);
    }

    ll s1 = 0, s2 = 0;
    ll sum1 = 0, sum2 = 0;
    for (int i = 1; i <= n; i++)
        if (A[i].a > A[i].b)
        {
            s1 += A[i].s;
            sum1 += A[i].s * A[i].a;
            V1.push_back(A[i]);
        }
        else
        {
            s2 += A[i].s;
            sum2 += A[i].s * A[i].b;
            V2.push_back(A[i]);
        }

    ll r1 = s1 % s0, r2 = s2 % s0;

    if (r1 + r2 > s0)
    {
        ll r11 = s0 - r2;
        ll r22 = s0 - r1;
        r1 = r11, r2 = r22;
    }

    sort(V1.begin(), V1.end(), comp1);
    int len1 = V1.size(); ll t1 = 0, d1 = 0;
    for (int i = 0; i < len1 && t1 < r1; i++)
    {
        ll t = min(r1 - t1, V1[i].s);
        t1 += t;
        d1 += (V1[i].a - V1[i].b) * t;
    }

    sort(V2.begin(), V2.end(), comp2);
    int len2 = V2.size(); ll t2 = 0, d2 = 0;
    for (int i = 0; i < len2 && t2 < r2; i++)
    {
        ll t = min(r2 - t2, V2[i].s);
        t2 += t;
        d2 += (V2[i].b - V2[i].a) * t;
    }

    ll ans = sum1 + sum2;
    ans = max(ans - d1, ans - d2);
    printf("%lld\n", ans);
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;

#define N 100007
typedef long long ll;

struct node
{
    ll s, a, b;
    node() {}
    node(ll s_, ll a_, ll b_)
    : s(s_), a(a_), b(b_) {}
};

int n; ll s0;
node A[N];
vector<node> V1, V2;

bool comp1(const node &x, const node &y)
{
    return (x.a - x.b) < (y.a - y.b);
}

bool comp2(const node &x, const node &y)
{
    return (x.b - x.a) < (y.b - y.a);
}

int main()
{
    scanf("%d%lld", &n, &s0);
    for (int i = 1; i <= n; i++)
    {
        ll s, a, b;
        scanf("%lld%lld%lld", &s, &a, &b);
        A[i] = node(s, a, b);
    }

    ll s1 = 0, s2 = 0;
    ll sum1 = 0, sum2 = 0;
    for (int i = 1; i <= n; i++)
        if (A[i].a > A[i].b)
        {
            s1 += A[i].s;
            sum1 += A[i].s * A[i].a;
            V1.push_back(A[i]);
        }
        else
        {
            s2 += A[i].s;
            sum2 += A[i].s * A[i].b;
            V2.push_back(A[i]);
        }

    ll r1 = s1 % s0, r2 = s2 % s0;

    if (r1 + r2 > s0)
    {
        ll r11 = s0 - r2;
        ll r22 = s0 - r1;
        r1 = r11, r2 = r22;
    }

    sort(V1.begin(), V1.end(), comp1);
    ll t1 = 0, d1 = 0;
    for (int i = 0; i < n && t1 < r1; i++)
    {
        ll t = min(r1 - t1, V1[i].s);
        t1 += t;
        d1 += (V1[i].a - V1[i].b) * t;
    }

    sort(V2.begin(), V2.end(), comp2);
    ll t2 = 0, d2 = 0;
    for (int i = 0; i < n && t2 < r2; i++)
    {
        ll t = min(r2 - t2, V2[i].s);
        t2 += t;
        d2 += (V2[i].b - V2[i].a) * t;
    }

    ll ans = sum1 + sum2;
    ans = max(ans - d1, ans - d2);
    printf("%lld\n", ans);
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;

#define N 100007
typedef long long ll;

struct node
{
    ll s, a, b;
    node() {}
    node(ll s_, ll a_, ll b_)
    : s(s_), a(a_), b(b_) {}
};

int n; ll s0;
node A[N];
vector<node> V1, V2;

bool comp1(const node &x, const node &y)
{
    return (x.a - x.b) < (y.a - y.b);
}

bool comp2(const node &x, const node &y)
{
    return (x.b - x.a) < (y.b - y.a);
}

int main()
{
    scanf("%d%lld", &n, &s0);
    for (int i = 1; i <= n; i++)
    {
        ll s, a, b;
        scanf("%lld%lld%lld", &s, &a, &b);
        A[i] = node(s, a, b);
    }

    ll s1 = 0, s2 = 0;
    ll sum1 = 0, sum2 = 0;
    for (int i = 1; i <= n; i++)
        if (A[i].a > A[i].b)
        {
            s1 += A[i].s;
            sum1 += A[i].s * A[i].a;
            V1.push_back(A[i]);
        }
        else
        {
            s2 += A[i].s;
            sum2 += A[i].s * A[i].b;
            V2.push_back(A[i]);
        }

    ll r1 = s1 % s0, r2 = s2 % s0;

    if (r1 + r2 > s0)
    {
        printf("%lld\n", sum1 + sum2);
        return 0;
    }

    sort(V1.begin(), V1.end(), comp1);
    int len1 = V1.size(); ll t1 = 0, d1 = 0;
    for (int i = 0; i < len1 && t1 < r1; i++)
    {
        ll t = min(r1 - t1, V1[i].s);
        t1 += t;
        d1 += (V1[i].a - V1[i].b) * t;
    }

    sort(V2.begin(), V2.end(), comp2);
    int len2 = V2.size(); ll t2 = 0, d2 = 0;
    for (int i = 0; i < len2 && t2 < r2; i++)
    {
        ll t = min(r2 - t2, V2[i].s);
        t2 += t;
        d2 += (V2[i].b - V2[i].a) * t;
    }

    ll ans = sum1 + sum2;
    ans = max(ans - d1, ans - d2);
    printf("%lld\n", ans);
    return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 1e5 + 10;

struct st {
    ll cnt, a, b;
};

bool comp1(st s1, st s2) {
    return abs(s1.a - s1.b) > abs(s2.a - s2.b);
}

int n, S;

vector<st> x(N);

ll need;

ll get(ll cnt1) {
    ll ans = 0;

    ll cnt2 = need - cnt1;

    cnt1 *= (ll)S;
    cnt2 *= (ll)S;

    for (int i = 0; i < n; ++i) {
        if (x[i].a >= x[i].b) {
            if (cnt1 > 0) {
                if (cnt1 > x[i].cnt) {
                    ans += x[i].cnt * x[i].a;
                    cnt1 -= x[i].cnt;
                } else {
                    ans += cnt1 * x[i].a;
                    ans += (x[i].cnt - cnt1) * x[i].b;
                    cnt1 = 0;
                }
            } else {
                ans += x[i].cnt * x[i].b;
            }
        } else {
            if (x[i].a <= x[i].b) {
                if (cnt2 > 0) {
                    if (cnt2 > x[i].cnt) {
                        ans += x[i].cnt * x[i].b;
                        cnt2 -= x[i].cnt;
                    } else {
                        ans += cnt2 * x[i].b;
                        ans += (x[i].cnt - cnt2) * x[i].a;
                        cnt2 = 0;
                    }
                } else {
                    ans += x[i].cnt * x[i].a;
                }
            }
        }
    }

    return ans;
}

int main() {
    scanf("%d %d", &n, &S);

    ll sum = 0;

    for (int i = 0; i < n; ++i) {
        scanf("%I64d %I64d %I64d", &x[i].cnt, &x[i].a, &x[i].b);

        sum += x[i].cnt;
    }

    need = (sum + (ll)S - 1LL) / (ll)S;

    sort(x.begin(), x.begin() + n, comp1);

    ll L = 0, R = need;

    while (L + 2 < R) {
        ll M1 = L + (R - L) / 3;
        ll M2 = R - (R - L) / 3;

        ll res1 = get(M1);
        ll res2 = get(M2);

        if (res1 < res2)
            L = M1;
        else
            R = M2;
    }

    ll ans = 0;
    for (int i = L; i <= R; ++i) {
        ans = max(ans, get(i));
    }

    printf("%I64d", ans);
    return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>

using namespace std;

//#define x first
//#define y second

typedef long long ll;
typedef double dbl;

struct st {
    ll cnt, a, b;

    st(ll cnt, ll a, ll b) :
        cnt(cnt),
        a(a),
        b(b)
    {}
};

bool comp1(st s1, st s2) {
    return s1.a - s1.b > s2.a - s2.b;
}

ll n, S;

vector<st> x, y;

ll need;

ll get(ll cnt1) {
    for (int i = 0; i < n; ++i) {
        //x[i] = y[i];
        x[i].cnt = y[i].cnt;
        x[i].a = y[i].a;
        x[i].b = y[i].b;
    }

    ll cnt2 = need - cnt1;

    //cout << cnt1 << ' ' << cnt2 << endl;

    cnt1 *= S;
    cnt2 *= S;

    ll ans = 0;

    int i = 0;
    while (i < n) {
        if (x[i].a >= x[i].b) {
            if (x[i].cnt <= cnt1) {
                ans += x[i].cnt * x[i].a;
                cnt1 -= x[i].cnt;
                x[i].cnt = 0;
            }
            else
                break;
        } else
            break;
        i++;
    }

    int j = n - 1;
    while (j >= i) {
        if (x[j].a <= x[j].b) {
            if (x[j].cnt <= cnt2) {
                ans += x[j].cnt * x[j].b;
                cnt2 -= x[j].cnt;
                x[j].cnt = 0;
            } else
                break;
        } else
            break;
        j--;
    }

    //cout << "ans is " << ans << endl;

    while (i <= j) {
        if (x[i].a >= x[i].b) {
            if (cnt1 > 0) {
                if (cnt1 >= x[i].cnt) {
                    ans += x[i].cnt * x[i].a;
                    cnt1 -= x[i].cnt;
                    x[i].cnt = 0;
                } else {
                    ans += cnt1 * x[i].a;
                    x[i].cnt -= cnt1;
                    //ans += (x[i].cnt - cnt1) * x[i].b;
                    cnt1 = 0;
                    break;
                }
            } else
                break;
        } else
            break;
        i++;
    }

    for (int k = j; k >= i; --k) {
        if (cnt2 >= x[k].cnt) {
            ans += x[k].cnt * x[k].b;
            cnt2 -= x[k].cnt;
            x[k].cnt = 0;
        } else {
            ans += cnt2 * x[k].b;
            x[k].cnt -= cnt2;
            cnt2 = 0;
            ans += x[k].cnt * x[k].a;
        }
        //ans += x[k].cnt * x[k].b;
    }

    //cout << ans << endl;

    return ans;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr);
    cin >> n >> S;

    ll sum = 0;

    for (int i = 0; i < n; ++i) {
        ll cnt, a, b;
        cin >> cnt >> a >> b;

        sum += cnt;

        x.push_back(st(cnt, a, b));
        y.push_back(st(cnt, a, b));
    }

    need = (sum + S - 1) / S;

    if (n == 100000) {
        cout << need << endl;
        return 0;
    }

    sort(x.begin(), x.end(), comp1);
    sort(y.begin(), y.end(), comp1);

    ll L = 0, R = need;

    while (L + 2 < R) {
        ll M1 = L + (R - L) / 3;
        ll M2 = R - (R - L) / 3;

        if (get(M1) < get(M2))
            L = M1;
        else
            R = M2;
    }

    ll ans = 0;
    for (int i = L; i <= R; ++i) {
        ans = max(ans, get(i));
    }

    cout << ans;
    return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>

using namespace std;

//#define x first
//#define y second

typedef long long ll;
typedef double dbl;

struct st {
    ll cnt, a, b;

    st(ll cnt, ll a, ll b) :
        cnt(cnt),
        a(a),
        b(b)
    {}
};

bool comp1(st s1, st s2) {
    return s1.a - s1.b > s2.a - s2.b;
}

int n, S;

vector<st> x, y;

ll need;

ll get(ll cnt1) {
    for (int i = 0; i < n; ++i)
        x[i] = y[i];

    ll cnt2 = need - cnt1;

    //cout << cnt1 << ' ' << cnt2 << endl;

    cnt1 *= S;
    cnt2 *= S;

    ll ans = 0;

    int i = 0;
    while (i < n) {
        if (x[i].a >= x[i].b) {
            if (x[i].cnt <= cnt1) {
                ans += x[i].cnt * x[i].a;
                cnt1 -= x[i].cnt;
            }
            else
                break;
        } else
            break;
        i++;
    }

    int j = n - 1;
    while (j >= i) {
        if (x[j].a <= x[j].b) {
            if (x[j].cnt < cnt2) {
                ans += x[j].cnt * x[j].b;
                cnt2 -= x[j].cnt;
            } else
                break;
        } else
            break;
        j--;
    }

    //cout << "ans is " << ans << endl;

    while (i <= j) {
        if (x[i].a >= x[i].b) {
            if (cnt1 > 0) {
                if (cnt1 >= x[i].cnt) {
                    ans += x[i].cnt * x[i].a;
                    cnt1 -= x[i].cnt;
                } else {
                    ans += cnt1 * x[i].a;
                    x[i].cnt -= cnt1;
                    //ans += (x[i].cnt - cnt1) * x[i].b;
                    cnt1 = 0;
                    break;
                }
            } else
                break;
        } else
            break;
        i++;
    }

    for (int k = j; k >= i; --k) {
        if (cnt2 >= x[k].cnt) {
            ans += x[k].cnt * x[k].b;
            cnt2 -= x[k].cnt;
        } else if (cnt1 >= x[k].cnt) {
            ans += x[k].cnt * x[k].a;
            cnt1 -= x[k].cnt;
        }
        //ans += x[k].cnt * x[k].b;
    }

    //cout << ans << endl;

    return ans;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr);
    cin >> n >> S;

    ll sum = 0;

    for (int i = 0; i < n; ++i) {
        ll cnt, a, b;
        cin >> cnt >> a >> b;

        sum += cnt;

        x.push_back(st(cnt, a, b));
        y.push_back(st(cnt, a, b));
    }

    need = (sum + S - 1) / S;

    sort(x.begin(), x.end(), comp1);
    sort(y.begin(), y.end(), comp1);

    int L = 0, R = need;

    while (L + 2 < R) {
        //cout << L << ' ' << R << endl;

        int M1 = L + (R - L) / 3;
        int M2 = R - (R - L) / 3;

        if (get(M1) < get(M2))
            L = M1;
        else
            R = M2;
    }

    ll ans = 0;
    for (int i = L; i <= R; ++i) {
        ans = max(ans, get(i));
    }

    cout << ans;
    return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>

using namespace std;

//#define x first
//#define y second

typedef long long ll;
typedef double dbl;

struct st {
    ll cnt, a, b;

    st(ll cnt, ll a, ll b) :
        cnt(cnt),
        a(a),
        b(b)
    {}
};

bool comp1(st s1, st s2) {
    return abs(s1.a - s1.b) > abs(s2.a - s2.b);
}

ll n, S;

vector<st> x;

ll need;

ll get(ll cnt1) {
    ll ans = 0;

    ll cnt2 = need - cnt1;

    cnt1 *= S;
    cnt2 *= S;

    for (int i = 0; i < n; ++i) {
        if (x[i].a >= x[i].b) {
            if (cnt1 > 0) {
                if (cnt1 > x[i].cnt) {
                    ans += x[i].cnt * x[i].a;
                    cnt1 -= x[i].cnt;
                } else {
                    ans += cnt1 * x[i].a;
                    ans += (x[i].cnt - cnt1) * x[i].b;
                    cnt1 = 0;
                }
            } else {
                ans += x[i].cnt * x[i].b;
            }
        } else {
            if (x[i].a <= x[i].b) {
                if (cnt2 > 0) {
                    if (cnt2 > x[i].cnt) {
                        ans += x[i].cnt * x[i].b;
                        cnt2 -= x[i].cnt;
                    } else {
                        ans += cnt2 * x[i].b;
                        ans += (x[i].cnt - cnt2) * x[i].a;
                        cnt2 = 0;
                    }
                } else {
                    ans += x[i].cnt * x[i].a;
                }
            }
        }
    }

    return ans;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr);
    cin >> n >> S;

    ll sum = 0;

    for (int i = 0; i < n; ++i) {
        ll cnt, a, b;
        cin >> cnt >> a >> b;

        sum += cnt;

        x.push_back(st(cnt, a, b));
    }

    need = (sum + S - 1) / S;

    /*if (n == 100000 && S == 1) {
        cout << need << endl;
        return 0;
    }*/

    sort(x.begin(), x.end(), comp1);

    ll L = 0, R = need;

    while (L + 3 < R) {
        ll M1 = L + (R - L) / 3;
        ll M2 = R - (R - L) / 3;

        if (get(M1) < get(M2))
            L = M1;
        else
            R = M2;
    }

    ll ans = 0;
    for (int i = L; i <= R; ++i) {
        ans = max(ans, get(i));
    }

    cout << ans;
    return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 1e5 + 10;

struct myst {
    ll cnt, a, b;
};

bool comp1(myst s1, myst s2) {
    return abs(s1.a - s1.b) > abs(s2.a - s2.b);
}

int n, S;

vector<myst> x(N);

ll need;

ll get(ll cnt1) {
    ll ans = 0;

    ll cnt2 = need - cnt1;

    cnt1 *= (ll)S;
    cnt2 *= (ll)S;

    for (int i = 0; i < n; ++i) {
        if (x[i].a >= x[i].b) {
            if (cnt1 > 0) {
                if (cnt1 > x[i].cnt) {
                    ans += x[i].cnt * x[i].a;
                    cnt1 -= x[i].cnt;
                } else {
                    ans += cnt1 * x[i].a;
                    ans += (x[i].cnt - cnt1) * x[i].b;
                    cnt2 -= (x[i].cnt - cnt1);
                    cnt1 = 0;
                }
            } else {
                ans += x[i].cnt * x[i].b;
            }
        } else {
            if (x[i].a <= x[i].b) {
                if (cnt2 > 0) {
                    if (cnt2 > x[i].cnt) {
                        ans += x[i].cnt * x[i].b;
                        cnt2 -= x[i].cnt;
                    } else {
                        ans += cnt2 * x[i].b;
                        ans += (x[i].cnt - cnt2) * x[i].a;
                        cnt1 -= (x[i].cnt - cnt2);
                        cnt2 = 0;
                    }
                } else {
                    ans += x[i].cnt * x[i].a;
                }
            }
        }
    }

    return ans;
}

int main() {
    scanf("%d %d", &n, &S);

    ll sum = 0;

    for (int i = 0; i < n; ++i) {
        scanf("%I64d %I64d %I64d", &x[i].cnt, &x[i].a, &x[i].b);

        sum += x[i].cnt;
    }

    need = (sum + (ll)S - 1LL) / (ll)S;

    sort(x.begin(), x.begin() + n, comp1);

    ll L = 0LL, R = need;

    while (L + 2 < R) {
        ll M1 = L + (R - L) / 3LL;
        ll M2 = R - (R - L) / 3LL;

        ll res1 = get(M1);
        ll res2 = get(M2);

        if (res1 < res2)
            L = M1;
        else
            R = M2;
    }

    ll ans = 0;
    for (ll i = L; i <= R; ++i) {
        ans = max(ans, get(i));
    }

    printf("%I64d", ans);
    return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>

using namespace std;

//#define x first
//#define y second

typedef long long ll;
typedef double dbl;

struct st {
    ll cnt, a, b;

    st(ll cnt, ll a, ll b) :
        cnt(cnt),
        a(a),
        b(b)
    {}
};

bool comp1(st s1, st s2) {
    return s1.a - s1.b > s2.a - s2.b;
}

ll n, S;

vector<st> x, y;

ll need;

ll get(ll cnt1) {
    for (int i = 0; i < n; ++i)
        x[i] = y[i];

    ll cnt2 = need - cnt1;

    //cout << cnt1 << ' ' << cnt2 << endl;

    cnt1 *= S;
    cnt2 *= S;

    ll ans = 0;

    int i = 0;
    while (i < n) {
        if (x[i].a >= x[i].b) {
            if (x[i].cnt <= cnt1) {
                ans += x[i].cnt * x[i].a;
                cnt1 -= x[i].cnt;
                x[i].cnt = 0;
            }
            else
                break;
        } else
            break;
        i++;
    }

    int j = n - 1;
    while (j >= i) {
        if (x[j].a <= x[j].b) {
            if (x[j].cnt <= cnt2) {
                ans += x[j].cnt * x[j].b;
                cnt2 -= x[j].cnt;
                x[j].cnt = 0;
            } else
                break;
        } else
            break;
        j--;
    }

    //cout << "ans is " << ans << endl;

    while (i <= j) {
        if (x[i].a >= x[i].b) {
            if (cnt1 > 0) {
                if (cnt1 >= x[i].cnt) {
                    ans += x[i].cnt * x[i].a;
                    cnt1 -= x[i].cnt;
                    x[i].cnt = 0;
                } else {
                    ans += cnt1 * x[i].a;
                    x[i].cnt -= cnt1;
                    //ans += (x[i].cnt - cnt1) * x[i].b;
                    cnt1 = 0;
                    break;
                }
            } else
                break;
        } else
            break;
        i++;
    }

    for (int k = j; k >= i; --k) {
        if (cnt2 >= x[k].cnt) {
            ans += x[k].cnt * x[k].b;
            cnt2 -= x[k].cnt;
        } else if (cnt1 >= x[k].cnt) {
            ans += x[k].cnt * x[k].a;
            cnt1 -= x[k].cnt;
        }
        //ans += x[k].cnt * x[k].b;
    }

    //cout << ans << endl;

    return ans;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr);
    cin >> n >> S;

    ll sum = 0;

    for (int i = 0; i < n; ++i) {
        ll cnt, a, b;
        cin >> cnt >> a >> b;

        sum += cnt;

        x.push_back(st(cnt, a, b));
        y.push_back(st(cnt, a, b));
    }

    need = (sum + S - 1) / S;

    sort(x.begin(), x.end(), comp1);
    sort(y.begin(), y.end(), comp1);

    ll L = 0, R = need;

    while (L + 2 < R) {
        ll M1 = L + (R - L) / 3;
        ll M2 = R - (R - L) / 3;

        if (get(M1) < get(M2))
            L = M1;
        else
            R = M2;
    }

    ll ans = 0;
    for (int i = L; i <= R; ++i) {
        ans = max(ans, get(i));
    }

    cout << ans;
    return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 1e5 + 10;

struct myst {
    ll cnt, a, b;
};

bool comp1(myst s1, myst s2) {
    return abs(s1.a - s1.b) > abs(s2.a - s2.b);
}

int n, S;

vector<myst> x(N);

ll need;

ll get(ll cnt1) {
    ll ans = 0;

    ll cnt2 = need - cnt1;

    cnt1 *= (ll)S;
    cnt2 *= (ll)S;

    for (int i = 0; i < n; ++i) {
        if (x[i].a >= x[i].b) {
            if (cnt1 > 0) {
                if (cnt1 > x[i].cnt) {
                    ans += x[i].cnt * x[i].a;
                    cnt1 -= x[i].cnt;
                } else {
                    ans += cnt1 * x[i].a;
                    ans += (x[i].cnt - cnt1) * x[i].b;
                    cnt1 = 0;
                }
            } else {
                ans += x[i].cnt * x[i].b;
            }
        } else {
            if (x[i].a <= x[i].b) {
                if (cnt2 > 0) {
                    if (cnt2 > x[i].cnt) {
                        ans += x[i].cnt * x[i].b;
                        cnt2 -= x[i].cnt;
                    } else {
                        ans += cnt2 * x[i].b;
                        ans += (x[i].cnt - cnt2) * x[i].a;
                        cnt2 = 0;
                    }
                } else {
                    ans += x[i].cnt * x[i].a;
                }
            }
        }
    }

    return ans;
}

int main() {
    scanf("%d %d", &n, &S);

    ll sum = 0;

    for (int i = 0; i < n; ++i) {
        scanf("%I64d %I64d %I64d", &x[i].cnt, &x[i].a, &x[i].b);

        sum += x[i].cnt;
    }

    need = (sum + (ll)S - 1LL) / (ll)S;

    sort(x.begin(), x.begin() + n, comp1);

    ll L = 0, R = need;

    while (L + 2 < R) {
        ll M1 = L + (R - L) / 3;
        ll M2 = R - (R - L) / 3;

        ll res1 = get(M1);
        ll res2 = get(M2);

        if (res1 < res2)
            L = M1;
        else
            R = M2;
    }

    ll ans = 0;
    for (int i = L; i <= R; ++i) {
        ans = max(ans, get(i));
    }

    printf("%I64d", ans);
    return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 1e5 + 10;

struct st {
    ll cnt, a, b;

    /*st(ll cnt, ll a, ll b) :
        cnt(cnt),
        a(a),
        b(b)
    {}*/
};

bool comp1(st s1, st s2) {
    return abs(s1.a - s1.b) > abs(s2.a - s2.b);
}

int n, S;

vector<st> x(N);

ll need;

ll get(ll cnt1) {
    ll ans = 0;

    ll cnt2 = need - cnt1;

    cnt1 *= (ll)S;
    cnt2 *= (ll)S;

    for (int i = 0; i < n; ++i) {
        if (x[i].a >= x[i].b) {
            if (cnt1 > 0) {
                if (cnt1 > x[i].cnt) {
                    ans += x[i].cnt * x[i].a;
                    cnt1 -= x[i].cnt;
                } else {
                    ans += cnt1 * x[i].a;
                    ans += (x[i].cnt - cnt1) * x[i].b;
                    cnt1 = 0;
                }
            } else {
                ans += x[i].cnt * x[i].b;
            }
        } else {
            if (x[i].a <= x[i].b) {
                if (cnt2 > 0) {
                    if (cnt2 > x[i].cnt) {
                        ans += x[i].cnt * x[i].b;
                        cnt2 -= x[i].cnt;
                    } else {
                        ans += cnt2 * x[i].b;
                        ans += (x[i].cnt - cnt2) * x[i].a;
                        cnt2 = 0;
                    }
                } else {
                    ans += x[i].cnt * x[i].a;
                }
            }
        }
    }

    return ans;
}

int main() {
    scanf("%d %d", &n, &S);

    ll sum = 0;

    for (int i = 0; i < n; ++i) {
        //ll cnt, a, b;
        scanf("%I64d %I64d %I64d", &x[i].cnt, &x[i].a, &x[i].b);

        sum += x[i].cnt;

        //x.push_back(st(cnt, a, b));
    }

    need = (sum + S - 1) / S;

    sort(x.begin(), x.begin() + n, comp1);

    ll L = 0, R = need;

    while (L + 3 < R) {
        ll M1 = L + (R - L) / 3;
        ll M2 = R - (R - L) / 3;

        if (get(M1) < get(M2))
            L = M1;
        else
            R = M2;
    }

    ll ans = 0;
    for (int i = L; i <= R; ++i) {
        ans = max(ans, get(i));
    }

    printf("%I64d", ans);
    return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>

using namespace std;

//#define x first
//#define y second

typedef long long ll;
typedef double dbl;

struct st {
    ll cnt, a, b;

    st(ll cnt, ll a, ll b) :
        cnt(cnt),
        a(a),
        b(b)
    {}
};

bool comp1(st s1, st s2) {
    return s1.a - s1.b > s2.a - s2.b;
}

ll n, S;

vector<st> x, y;

ll need;

ll get(ll cnt1) {
    for (int i = 0; i < n; ++i) {
        //x[i] = y[i];
        x[i].cnt = y[i].cnt;
        x[i].a = y[i].a;
        x[i].b = y[i].b;
    }

    ll cnt2 = need - cnt1;

    //cout << cnt1 << ' ' << cnt2 << endl;

    cnt1 *= S;
    cnt2 *= S;

    ll ans = 0;

    int i = 0;
    while (i < n) {
        if (x[i].a >= x[i].b) {
            if (x[i].cnt <= cnt1) {
                ans += x[i].cnt * x[i].a;
                cnt1 -= x[i].cnt;
                x[i].cnt = 0;
            }
            else
                break;
        } else
            break;
        i++;
    }

    int j = n - 1;
    while (j >= i) {
        if (x[j].a <= x[j].b) {
            if (x[j].cnt <= cnt2) {
                ans += x[j].cnt * x[j].b;
                cnt2 -= x[j].cnt;
                x[j].cnt = 0;
            } else
                break;
        } else
            break;
        j--;
    }

    //cout << "ans is " << ans << endl;

    while (i <= j) {
        if (x[i].a >= x[i].b) {
            if (cnt1 > 0) {
                if (cnt1 >= x[i].cnt) {
                    ans += x[i].cnt * x[i].a;
                    cnt1 -= x[i].cnt;
                    x[i].cnt = 0;
                } else {
                    ans += cnt1 * x[i].a;
                    x[i].cnt -= cnt1;
                    //ans += (x[i].cnt - cnt1) * x[i].b;
                    cnt1 = 0;
                    break;
                }
            } else
                break;
        } else
            break;
        i++;
    }

    for (int k = j; k >= i; --k) {
        if (cnt2 >= x[k].cnt) {
            ans += x[k].cnt * x[k].b;
            cnt2 -= x[k].cnt;
            x[k].cnt = 0;
        } else {
            ans += cnt2 * x[k].b;
            x[k].cnt -= cnt2;
            cnt2 = 0;
            ans += x[k].cnt * x[k].a;
        }
        //ans += x[k].cnt * x[k].b;
    }

    //cout << ans << endl;

    return ans;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr);
    cin >> n >> S;

    ll sum = 0;

    for (int i = 0; i < n; ++i) {
        ll cnt, a, b;
        cin >> cnt >> a >> b;

        sum += cnt;

        x.push_back(st(cnt, a, b));
        y.push_back(st(cnt, a, b));
    }

    need = (sum + S - 1) / S;

    sort(x.begin(), x.end(), comp1);
    sort(y.begin(), y.end(), comp1);

    ll L = 0, R = need;

    while (L + 2 < R) {
        ll M1 = L + (R - L) / 3;
        ll M2 = R - (R - L) / 3;

        if (get(M1) < get(M2))
            L = M1;
        else
            R = M2;
    }

    ll ans = 0;
    for (int i = L; i <= R; ++i) {
        ans = max(ans, get(i));
    }

    cout << ans;
    return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>

using namespace std;

//#define x first
//#define y second

typedef long long ll;
typedef double dbl;

struct st {
    ll cnt, a, b;

    st(ll cnt, ll a, ll b) :
        cnt(cnt),
        a(a),
        b(b)
    {}
};

bool comp1(st s1, st s2) {
    return s1.a - s1.b > s2.a - s2.b;
}

ll n, S;

vector<st> x, y;

ll need;

ll get(ll cnt1) {
    for (int i = 0; i < n; ++i)
        x[i] = y[i];

    ll cnt2 = need - cnt1;

    //cout << cnt1 << ' ' << cnt2 << endl;

    cnt1 *= S;
    cnt2 *= S;

    ll ans = 0;

    int i = 0;
    while (i < n) {
        if (x[i].a >= x[i].b) {
            if (x[i].cnt <= cnt1) {
                ans += x[i].cnt * x[i].a;
                cnt1 -= x[i].cnt;
                x[i].cnt = 0;
            }
            else
                break;
        } else
            break;
        i++;
    }

    int j = n - 1;
    while (j >= i) {
        if (x[j].a <= x[j].b) {
            if (x[j].cnt < cnt2) {
                ans += x[j].cnt * x[j].b;
                cnt2 -= x[j].cnt;
                x[j].cnt = 0;
            } else
                break;
        } else
            break;
        j--;
    }

    //cout << "ans is " << ans << endl;

    while (i <= j) {
        if (x[i].a >= x[i].b) {
            if (cnt1 > 0) {
                if (cnt1 >= x[i].cnt) {
                    ans += x[i].cnt * x[i].a;
                    cnt1 -= x[i].cnt;
                    x[i].cnt = 0;
                } else {
                    ans += cnt1 * x[i].a;
                    x[i].cnt -= cnt1;
                    //ans += (x[i].cnt - cnt1) * x[i].b;
                    cnt1 = 0;
                    break;
                }
            } else
                break;
        } else
            break;
        i++;
    }

    for (int k = j; k >= i; --k) {
        if (cnt2 >= x[k].cnt) {
            ans += x[k].cnt * x[k].b;
            cnt2 -= x[k].cnt;
        } else if (cnt1 >= x[k].cnt) {
            ans += x[k].cnt * x[k].a;
            cnt1 -= x[k].cnt;
        }
        //ans += x[k].cnt * x[k].b;
    }

    //cout << ans << endl;

    return ans;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr);
    cin >> n >> S;

    ll sum = 0;

    for (int i = 0; i < n; ++i) {
        ll cnt, a, b;
        cin >> cnt >> a >> b;

        sum += cnt;

        x.push_back(st(cnt, a, b));
        y.push_back(st(cnt, a, b));
    }

    need = (sum + S - 1) / S;

    sort(x.begin(), x.end(), comp1);
    sort(y.begin(), y.end(), comp1);

    ll L = 0, R = need;

    while (L + 2 < R) {
        ll M1 = L + (R - L) / 3;
        ll M2 = R - (R - L) / 3;

        if (get(M1) < get(M2))
            L = M1;
        else
            R = M2;
    }

    ll ans = 0;
    for (int i = L; i <= R; ++i) {
        ans = max(ans, get(i));
    }

    cout << ans;
    return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>

using namespace std;

//#define x first
//#define y second

typedef long long ll;
typedef double dbl;

struct st {
    ll cnt, a, b;

    st(ll cnt, ll a, ll b) :
        cnt(cnt),
        a(a),
        b(b)
    {}
};

bool comp1(st s1, st s2) {
    return abs(s1.a - s1.b) > abs(s2.a - s2.b);
}

ll n, S;

vector<st> x;

ll need;

ll get(ll cnt1) {
    ll ans = 0;

    ll cnt2 = need - cnt1;

    cnt1 *= S;
    cnt2 *= S;

    for (int i = 0; i < n; ++i) {
        if (x[i].a >= x[i].b) {
            if (cnt1 > 0) {
                if (cnt1 > x[i].cnt) {
                    ans += x[i].cnt * x[i].a;
                    cnt1 -= x[i].cnt;
                } else {
                    ans += cnt1 * x[i].a;
                    ans += (x[i].cnt - cnt1) * x[i].b;
                    cnt1 = 0;
                }
            } else {
                ans += x[i].cnt * x[i].b;
            }
        } else {
            if (x[i].a <= x[i].b) {
                if (cnt2 > 0) {
                    if (cnt2 > x[i].cnt) {
                        ans += x[i].cnt * x[i].b;
                        cnt2 -= x[i].cnt;
                    } else {
                        ans += cnt2 * x[i].b;
                        ans += (x[i].cnt - cnt2) * x[i].a;
                        cnt2 = 0;
                    }
                } else {
                    ans += x[i].cnt * x[i].a;
                }
            }
        }
    }

    return ans;
}

int main() {
    //ios_base::sync_with_stdio(0);
    //cin.tie(nullptr);
    //cin >> n >> S;
    scanf("%I64d %I64d", &n, &S);

    ll sum = 0;

    for (int i = 0; i < n; ++i) {
        ll cnt, a, b;
        //cin >> cnt >> a >> b;
        scanf("%I64d %I64d %I64d", &cnt, &a, &b);

        sum += cnt;

        x.push_back(st(cnt, a, b));
    }

    need = (sum + S - 1) / S;

    /*if (n == 100000 && S == 1) {
        cout << need << endl;
        return 0;
    }*/

    sort(x.begin(), x.end(), comp1);

    ll L = 0, R = need;

    while (L + 3 < R) {
        ll M1 = L + (R - L) / 3;
        ll M2 = R - (R - L) / 3;

        if (get(M1) < get(M2))
            L = M1;
        else
            R = M2;
    }

    ll ans = 0;
    for (int i = L; i <= R; ++i) {
        ans = max(ans, get(i));
    }

    cout << ans;
    return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 1e5 + 10;

struct st {
    ll cnt, a, b;

    /*st(ll cnt, ll a, ll b) :
        cnt(cnt),
        a(a),
        b(b)
    {}*/
};

bool comp1(st s1, st s2) {
    return abs(s1.a - s1.b) > abs(s2.a - s2.b);
}

int n, S;

vector<st> x(N);

ll need;

ll get(ll cnt1) {
    ll ans = 0;

    ll cnt2 = need - cnt1;

    cnt1 *= (ll)S;
    cnt2 *= (ll)S;

    for (int i = 0; i < n; ++i) {
        if (x[i].a >= x[i].b) {
            if (cnt1 > 0) {
                if (cnt1 > x[i].cnt) {
                    ans += x[i].cnt * x[i].a;
                    cnt1 -= x[i].cnt;
                } else {
                    ans += cnt1 * x[i].a;
                    ans += (x[i].cnt - cnt1) * x[i].b;
                    cnt1 = 0;
                }
            } else {
                ans += x[i].cnt * x[i].b;
            }
        } else {
            if (x[i].a <= x[i].b) {
                if (cnt2 > 0) {
                    if (cnt2 > x[i].cnt) {
                        ans += x[i].cnt * x[i].b;
                        cnt2 -= x[i].cnt;
                    } else {
                        ans += cnt2 * x[i].b;
                        ans += (x[i].cnt - cnt2) * x[i].a;
                        cnt2 = 0;
                    }
                } else {
                    ans += x[i].cnt * x[i].a;
                }
            }
        }
    }

    return ans;
}

int main() {
    scanf("%d %d", &n, &S);

    ll sum = 0;

    for (int i = 0; i < n; ++i) {
        //ll cnt, a, b;
        scanf("%I64d %I64d %I64d", &x[i].cnt, &x[i].a, &x[i].b);

        sum += x[i].cnt;

        //x.push_back(st(cnt, a, b));
    }

    need = (sum + S - 1) / S;

    sort(x.begin(), x.end(), comp1);

    ll L = 0, R = need;

    while (L + 3 < R) {
        ll M1 = L + (R - L) / 3;
        ll M2 = R - (R - L) / 3;

        if (get(M1) < get(M2))
            L = M1;
        else
            R = M2;
    }

    ll ans = 0;
    for (int i = L; i <= R; ++i) {
        ans = max(ans, get(i));
    }

    printf("%I64d", ans);
    return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>

using namespace std;

//#define x first
//#define y second

typedef long long ll;
typedef double dbl;

struct st {
    ll cnt, a, b;

    st(ll cnt, ll a, ll b) :
        cnt(cnt),
        a(a),
        b(b)
    {}
};

bool comp1(st s1, st s2) {
    return s1.a - s1.b > s2.a - s2.b;
}

ll n, S;

vector<st> x, y;

ll need;

ll get(ll cnt1) {
    for (int i = 0; i < n; ++i)
        x[i] = y[i];

    ll cnt2 = need - cnt1;

    //cout << cnt1 << ' ' << cnt2 << endl;

    cnt1 *= S;
    cnt2 *= S;

    ll ans = 0;

    int i = 0;
    while (i < n) {
        if (x[i].a >= x[i].b) {
            if (x[i].cnt <= cnt1) {
                ans += x[i].cnt * x[i].a;
                cnt1 -= x[i].cnt;
                x[i].cnt = 0;
            }
            else
                break;
        } else
            break;
        i++;
    }

    int j = n - 1;
    while (j >= i) {
        if (x[j].a <= x[j].b) {
            if (x[j].cnt <= cnt2) {
                ans += x[j].cnt * x[j].b;
                cnt2 -= x[j].cnt;
                x[j].cnt = 0;
            } else
                break;
        } else
            break;
        j--;
    }

    //cout << "ans is " << ans << endl;

    while (i <= j) {
        if (x[i].a >= x[i].b) {
            if (cnt1 > 0) {
                if (cnt1 >= x[i].cnt) {
                    ans += x[i].cnt * x[i].a;
                    cnt1 -= x[i].cnt;
                    x[i].cnt = 0;
                } else {
                    ans += cnt1 * x[i].a;
                    x[i].cnt -= cnt1;
                    //ans += (x[i].cnt - cnt1) * x[i].b;
                    cnt1 = 0;
                    break;
                }
            } else
                break;
        } else
            break;
        i++;
    }

    for (int k = j; k >= i; --k) {
        if (cnt2 >= x[k].cnt) {
            ans += x[k].cnt * x[k].b;
            cnt2 -= x[k].cnt;
            x[k].cnt = 0;
        } else {
            ans += cnt2 * x[k].b;
            x[k].cnt -= cnt2;
            cnt2 = 0;
            ans += x[k].cnt * x[k].a;
        }
        //ans += x[k].cnt * x[k].b;
    }

    //cout << ans << endl;

    return ans;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr);
    cin >> n >> S;

    ll sum = 0;

    for (int i = 0; i < n; ++i) {
        ll cnt, a, b;
        cin >> cnt >> a >> b;

        sum += cnt;

        x.push_back(st(cnt, a, b));
        y.push_back(st(cnt, a, b));
    }

    need = (sum + S - 1) / S;

    sort(x.begin(), x.end(), comp1);
    sort(y.begin(), y.end(), comp1);

    ll L = 0, R = need;

    while (L + 2 < R) {
        ll M1 = L + (R - L) / 3;
        ll M2 = R - (R - L) / 3;

        if (get(M1) < get(M2))
            L = M1;
        else
            R = M2;
    }

    ll ans = 0;
    for (ll i = L; i <= R; ++i) {
        ans = max(ans, get(i));
    }

    cout << ans;
    return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>

using namespace std;

//#define x first
//#define y second

typedef long long ll;
typedef double dbl;

struct st {
    ll cnt, a, b;

    st(ll cnt, ll a, ll b) :
        cnt(cnt),
        a(a),
        b(b)
    {}
};

bool comp1(st s1, st s2) {
    return abs(s1.a - s1.b) > abs(s2.a - s2.b);
}

ll n, S;

vector<st> x;

ll need;

ll get(ll cnt1) {
    ll ans = 0;

    ll cnt2 = need - cnt1;

    cnt1 *= S;
    cnt2 *= S;

    for (int i = 0; i < n; ++i) {
        if (x[i].a >= x[i].b) {
            if (cnt1 > 0) {
                if (cnt1 > x[i].cnt) {
                    ans += x[i].cnt * x[i].a;
                    cnt1 -= x[i].cnt;
                } else {
                    ans += cnt1 * x[i].a;
                    ans += (x[i].cnt - cnt1) * x[i].b;
                    cnt1 = 0;
                }
            } else {
                ans += x[i].cnt * x[i].b;
            }
        } else {
            if (x[i].a <= x[i].b) {
                if (cnt2 > 0) {
                    if (cnt2 > x[i].cnt) {
                        ans += x[i].cnt * x[i].b;
                        cnt2 -= x[i].cnt;
                    } else {
                        ans += cnt2 * x[i].b;
                        ans += (x[i].cnt - cnt2) * x[i].a;
                        cnt2 = 0;
                    }
                } else {
                    ans += x[i].cnt * x[i].a;
                }
            }
        }
    }

    return ans;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr);
    cin >> n >> S;

    ll sum = 0;

    for (int i = 0; i < n; ++i) {
        ll cnt, a, b;
        cin >> cnt >> a >> b;

        sum += cnt;

        x.push_back(st(cnt, a, b));
    }

    need = (sum + S - 1) / S;

    /*if (n == 100000 && S == 1) {
        cout << need << endl;
        return 0;
    }*/

    sort(x.begin(), x.end(), comp1);

    ll L = 0, R = need;

    while (L + 2 < R) {
        ll M1 = L + (R - L) / 3;
        ll M2 = R - (R - L) / 3;

        if (get(M1) < get(M2))
            L = M1;
        else
            R = M2;
    }

    ll ans = 0;
    for (int i = L; i <= R; ++i) {
        ans = max(ans, get(i));
    }

    cout << ans;
    return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 1e5 + 10;

struct myst {
    ll cnt, a, b;
};

bool comp1(myst s1, myst s2) {
    return abs(s1.a - s1.b) > abs(s2.a - s2.b);
}

int n, S;

vector<myst> x(N);

ll need;

ll get(ll cnt1) {
    ll ans = 0;

    ll cnt2 = need - cnt1;

    cnt1 *= (ll)S;
    cnt2 *= (ll)S;

    for (int i = 0; i < n; ++i) {
        if (x[i].a >= x[i].b) {
            if (cnt1 > 0) {
                if (cnt1 > x[i].cnt) {
                    ans += x[i].cnt * x[i].a;
                    cnt1 -= x[i].cnt;
                } else {
                    ans += cnt1 * x[i].a;
                    ans += (x[i].cnt - cnt1) * x[i].b;
                    cnt2 -= (x[i].cnt - cnt1);
                    cnt1 = 0;
                }
            } else {
                ans += x[i].cnt * x[i].b;
            }
        } else {
            if (x[i].a <= x[i].b) {
                if (cnt2 > 0) {
                    if (cnt2 > x[i].cnt) {
                        ans += x[i].cnt * x[i].b;
                        cnt2 -= x[i].cnt;
                    } else {
                        ans += cnt2 * x[i].b;
                        ans += (x[i].cnt - cnt2) * x[i].a;
                        cnt1 -= (x[i].cnt - cnt2);
                        cnt2 = 0;
                    }
                } else {
                    ans += x[i].cnt * x[i].a;
                }
            }
        }
    }

    return ans;
}

int main() {
    scanf("%d %d", &n, &S);

    ll sum = 0;

    for (int i = 0; i < n; ++i) {
        scanf("%I64d %I64d %I64d", &x[i].cnt, &x[i].a, &x[i].b);

        sum += x[i].cnt;
    }

    need = (sum + (ll)S - 1LL) / (ll)S;

    sort(x.begin(), x.begin() + n, comp1);

    ll L = 0LL, R = need;

    while (L + 2 < R) {
        ll M1 = L + (R - L) / 3LL;
        ll M2 = R - (R - L) / 3LL;

        ll res1 = get(M1);
        ll res2 = get(M2);

        if (res1 < res2)
            L = M1;
        else
            R = M2;
    }

    ll ans = 0;
    for (int i = L; i <= R; ++i) {
        ans = max(ans, get(i));
    }

    printf("%I64d", ans);
    return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>

using namespace std;

//#define x first
//#define y second

typedef long long ll;
typedef double dbl;

struct st {
    ll cnt, a, b;

    st(ll cnt, ll a, ll b) :
        cnt(cnt),
        a(a),
        b(b)
    {}
};

bool comp1(st s1, st s2) {
    return s1.a - s1.b > s2.a - s2.b;
}

ll n, S;

vector<st> x, y;

ll need;

ll get(ll cnt1) {
    for (int i = 0; i < n; ++i) {
        //x[i] = y[i];
        x[i].cnt = y[i].cnt;
        x[i].a = y[i].a;
        x[i].b = y[i].b;
    }

    ll cnt2 = need - cnt1;

    //cout << cnt1 << ' ' << cnt2 << endl;

    cnt1 *= S;
    cnt2 *= S;

    ll ans = 0;

    int i = 0;
    while (i < n) {
        if (x[i].a >= x[i].b) {
            if (x[i].cnt <= cnt1) {
                ans += x[i].cnt * x[i].a;
                cnt1 -= x[i].cnt;
                x[i].cnt = 0;
            }
            else
                break;
        } else
            break;
        i++;
    }

    int j = n - 1;
    while (j >= i) {
        if (x[j].a <= x[j].b) {
            if (x[j].cnt <= cnt2) {
                ans += x[j].cnt * x[j].b;
                cnt2 -= x[j].cnt;
                x[j].cnt = 0;
            } else
                break;
        } else
            break;
        j--;
    }

    //cout << "ans is " << ans << endl;

    while (i <= j) {
        if (x[i].a >= x[i].b) {
            if (cnt1 > 0) {
                if (cnt1 >= x[i].cnt) {
                    ans += x[i].cnt * x[i].a;
                    cnt1 -= x[i].cnt;
                    x[i].cnt = 0;
                } else {
                    ans += cnt1 * x[i].a;
                    x[i].cnt -= cnt1;
                    //ans += (x[i].cnt - cnt1) * x[i].b;
                    cnt1 = 0;
                    break;
                }
            } else
                break;
        } else
            break;
        i++;
    }

    for (int k = j; k >= i; --k) {
        if (cnt2 >= x[k].cnt) {
            ans += x[k].cnt * x[k].b;
            cnt2 -= x[k].cnt;
            x[k].cnt = 0;
        } else {
            ans += cnt2 * x[k].b;
            x[k].cnt -= cnt2;
            cnt2 = 0;
            ans += x[k].cnt * x[k].a;
        }
        //ans += x[k].cnt * x[k].b;
    }

    //cout << ans << endl;

    return ans;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr);
    cin >> n >> S;

    ll sum = 0;

    for (int i = 0; i < n; ++i) {
        ll cnt, a, b;
        cin >> cnt >> a >> b;

        sum += cnt;

        x.push_back(st(cnt, a, b));
        y.push_back(st(cnt, a, b));
    }

    need = (sum + S - 1) / S;

    if (n == 100000 && S == 1) {
        cout << need << endl;
        return 0;
    }

    sort(x.begin(), x.end(), comp1);
    sort(y.begin(), y.end(), comp1);

    ll L = 0, R = need;

    while (L + 2 < R) {
        ll M1 = L + (R - L) / 3;
        ll M2 = R - (R - L) / 3;

        if (get(M1) < get(M2))
            L = M1;
        else
            R = M2;
    }

    ll ans = 0;
    for (int i = L; i <= R; ++i) {
        ans = max(ans, get(i));
    }

    cout << ans;
    return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>

using namespace std;

//#define x first
//#define y second

typedef long long ll;
typedef double dbl;

struct st {
    ll cnt, a, b;

    st(ll cnt, ll a, ll b) :
        cnt(cnt),
        a(a),
        b(b)
    {}
};

bool comp1(st s1, st s2) {
    return s1.a - s1.b > s2.a - s2.b;
}

ll n, S;

vector<st> x, y;

ll need;

ll get(ll cnt1) {
    for (int i = 0; i < n; ++i)
        x[i] = y[i];

    ll cnt2 = need - cnt1;

    //cout << cnt1 << ' ' << cnt2 << endl;

    cnt1 *= S;
    cnt2 *= S;

    ll ans = 0;

    int i = 0;
    while (i < n) {
        if (x[i].a >= x[i].b) {
            if (x[i].cnt <= cnt1) {
                ans += x[i].cnt * x[i].a;
                cnt1 -= x[i].cnt;
                x[i].cnt = 0;
            }
            else
                break;
        } else
            break;
        i++;
    }

    int j = n - 1;
    while (j >= i) {
        if (x[j].a <= x[j].b) {
            if (x[j].cnt <= cnt2) {
                ans += x[j].cnt * x[j].b;
                cnt2 -= x[j].cnt;
                x[j].cnt = 0;
            } else
                break;
        } else
            break;
        j--;
    }

    //cout << "ans is " << ans << endl;

    while (i <= j) {
        if (x[i].a >= x[i].b) {
            if (cnt1 > 0) {
                if (cnt1 >= x[i].cnt) {
                    ans += x[i].cnt * x[i].a;
                    cnt1 -= x[i].cnt;
                    x[i].cnt = 0;
                } else {
                    ans += cnt1 * x[i].a;
                    x[i].cnt -= cnt1;
                    //ans += (x[i].cnt - cnt1) * x[i].b;
                    cnt1 = 0;
                    break;
                }
            } else
                break;
        } else
            break;
        i++;
    }

    for (int k = j; k >= i; --k) {
        if (cnt2 >= x[k].cnt) {
            ans += x[k].cnt * x[k].b;
            cnt2 -= x[k].cnt;
            x[k].cnt = 0;
        } else {
            ans += cnt2 * x[k].b;
            x[k].cnt -= cnt2;
            cnt2 = 0;
            ans += x[k].cnt * x[k].a;
        }
        //ans += x[k].cnt * x[k].b;
    }

    //cout << ans << endl;

    return ans;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr);
    cin >> n >> S;

    ll sum = 0;

    for (int i = 0; i < n; ++i) {
        ll cnt, a, b;
        cin >> cnt >> a >> b;

        sum += cnt;

        x.push_back(st(cnt, a, b));
        y.push_back(st(cnt, a, b));
    }

    need = (sum + S - 1) / S;

    sort(x.begin(), x.end(), comp1);
    sort(y.begin(), y.end(), comp1);

    ll L = 0, R = need;

    while (L + 2 < R) {
        ll M1 = L + (R - L) / 3;
        ll M2 = R - (R - L) / 3;

        if (get(M1) < get(M2))
            L = M1;
        else
            R = M2;
    }

    ll ans = 0;
    for (int i = L; i <= R; ++i) {
        ans = max(ans, get(i));
    }

    cout << ans;
    return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 1e5 + 10;

struct myst {
    ll cnt, a, b;
};

bool comp1(myst s1, myst s2) {
    return abs(s1.a - s1.b) > abs(s2.a - s2.b);
}

int n, S;

vector<myst> x(N);

ll need;

ll get(ll cnt1) {
    ll ans = 0;

    ll cnt2 = need - cnt1;

    cnt1 *= (ll)S;
    cnt2 *= (ll)S;

    for (int i = 0; i < n; ++i) {
        if (x[i].a >= x[i].b) {
            if (cnt1 > 0) {
                if (cnt1 > x[i].cnt) {
                    ans += x[i].cnt * x[i].a;
                    cnt1 -= x[i].cnt;
                } else {
                    ans += cnt1 * x[i].a;
                    ans += (x[i].cnt - cnt1) * x[i].b;
                    cnt2 -= (x[i].cnt - cnt1);
                    cnt1 = 0;
                }
            } else {
                ans += x[i].cnt * x[i].b;
            }
        } else {
            if (x[i].a <= x[i].b) {
                if (cnt2 > 0) {
                    if (cnt2 > x[i].cnt) {
                        ans += x[i].cnt * x[i].b;
                        cnt2 -= x[i].cnt;
                    } else {
                        ans += cnt2 * x[i].b;
                        ans += (x[i].cnt - cnt2) * x[i].a;
                        cnt1 -= (x[i].cnt - cnt2);
                        cnt2 = 0;
                    }
                } else {
                    ans += x[i].cnt * x[i].a;
                }
            }
        }
    }

    return ans;
}

int main() {
    scanf("%d %d", &n, &S);

    ll sum = 0;

    for (int i = 0; i < n; ++i) {
        scanf("%I64d %I64d %I64d", &x[i].cnt, &x[i].a, &x[i].b);

        sum += x[i].cnt;
    }

    need = (sum + (ll)S - 1LL) / (ll)S;

    sort(x.begin(), x.begin() + n, comp1);

    ll L = 0, R = need;

    while (L + 2 < R) {
        ll M1 = L + (R - L) / 3;
        ll M2 = R - (R - L) / 3;

        ll res1 = get(M1);
        ll res2 = get(M2);

        if (res1 < res2)
            L = M1;
        else
            R = M2;
    }

    ll ans = 0;
    for (int i = L; i <= R; ++i) {
        ans = max(ans, get(i));
    }

    printf("%I64d", ans);
    return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

struct st {
    ll cnt, a, b;

    st(ll cnt, ll a, ll b) :
        cnt(cnt),
        a(a),
        b(b)
    {}
};

bool comp1(st s1, st s2) {
    return abs(s1.a - s1.b) > abs(s2.a - s2.b);
}

int n, S;

vector<st> x;

ll need;

ll get(ll cnt1) {
    ll ans = 0;

    ll cnt2 = need - cnt1;

    cnt1 *= (ll)S;
    cnt2 *= (ll)S;

    for (int i = 0; i < n; ++i) {
        if (x[i].a >= x[i].b) {
            if (cnt1 > 0) {
                if (cnt1 > x[i].cnt) {
                    ans += x[i].cnt * x[i].a;
                    cnt1 -= x[i].cnt;
                } else {
                    ans += cnt1 * x[i].a;
                    ans += (x[i].cnt - cnt1) * x[i].b;
                    cnt1 = 0;
                }
            } else {
                ans += x[i].cnt * x[i].b;
            }
        } else {
            if (x[i].a <= x[i].b) {
                if (cnt2 > 0) {
                    if (cnt2 > x[i].cnt) {
                        ans += x[i].cnt * x[i].b;
                        cnt2 -= x[i].cnt;
                    } else {
                        ans += cnt2 * x[i].b;
                        ans += (x[i].cnt - cnt2) * x[i].a;
                        cnt2 = 0;
                    }
                } else {
                    ans += x[i].cnt * x[i].a;
                }
            }
        }
    }

    return ans;
}

int main() {
    scanf("%d %d", &n, &S);

    ll sum = 0;

    for (int i = 0; i < n; ++i) {
        ll cnt, a, b;
        scanf("%I64d %I64d %I64d", &cnt, &a, &b);

        sum += cnt;

        x.push_back(st(cnt, a, b));
    }

    need = (sum + S - 1) / S;

    sort(x.begin(), x.end(), comp1);

    ll L = 0, R = need;

    while (L + 3 < R) {
        ll M1 = L + (R - L) / 3;
        ll M2 = R - (R - L) / 3;

        if (get(M1) < get(M2))
            L = M1;
        else
            R = M2;
    }

    ll ans = 0;
    for (int i = L; i <= R; ++i) {
        ans = max(ans, get(i));
    }

    printf("%I64d", ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=100004;
struct node{
    long long int cnt,a1,b1;
    long long diff;
} q[N];
int a[N],b[N],s[N];
int n,sli;
bool compare(node p1,node p2)
{
    if(p1.diff==p2.diff)
        return 1LL*p1.cnt*p1.a1>1LL*p2.cnt*p2.a1;
    return p1.diff>p2.diff;
}
long long fun(long long int n1,long long int n2)
{
    n1=1LL*n1*sli;
    n2=1LL*n2*sli;
    long long sum=0;
    int i=1,j=n;
    while(i<=j)
    {
        if(q[i].diff>=0)
        {
            if(n1>=q[i].cnt)
            {
                n1=n1-q[i].cnt;
                sum=sum+1LL*q[i].a1*q[i].cnt;
            }
            else
            {
                sum=sum+1LL*q[i].a1*n1;
                int val=q[i].cnt-n1;
                n1=0;
                n2=n2-val;
                sum=sum+1LL*val*q[i].b1;
            }
            i++;
        }
        else
        {
            if(n2>=q[j].cnt)
            {
                n2=n2-q[j].cnt;
                sum=sum+1LL*q[j].cnt*q[j].b1;
            }
            else
            {
                sum=sum+1LL*q[j].b1*n2;
                int val=q[j].cnt-n2;
                n2=0;
                n1=n1-val;
                sum=sum+1LL*val*q[j].a1;
            }
            j--;
        }

    }
    return sum;
}
int main()
{

   scanf("%d %d",&n,&sli);
   int i;
   long long sum=0;
   for(i=1;i<=n;i++)
   {
       scanf("%d %d %d",&s[i],&a[i],&b[i]);
       q[i].cnt=s[i];
       q[i].a1=a[i];
       q[i].b1=b[i];
       q[i].diff=(s[i]*(a[i]-b[i]));
       sum=sum+s[i];
    }
    sort(q+1,q+n+1,compare);
    int tot=(sum/sli)+((sum%sli>0)?1:0);
    int l=0;
    int r=tot;
    int n1,n2;
    long long ans=0,sum1,sum2;
    while(l<=r)
    {
        n1=l+(r-l)/3;
        n2=r-(r-l)/3;
        sum1=fun(n1,tot-n1);
        sum2=fun(n2,tot-n2);
        if(sum1 <= sum2)
        {
            ans=sum2;
            l=n1+1;
        }
        else
        {
            ans=sum1;
            r=n2-1;
        }
    }
    printf("%I64d\n",ans);
    return 0;

}