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

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

Input

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

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

Output

Print the maximum total happiness that can be achieved.

Examples

Input

3 12
3 5 7
4 6 7
5 9 5


Output

84


Input

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


Output

314

Note

In the first example, you only need to buy one pizza. If you buy a type 1 pizza, the total happiness will be 3·5 + 4·6 + 5·9 = 84, and if you buy a type 2 pizza, the total happiness will be 3·7 + 4·7 + 5·5 = 74.
import java.io.* ;
import java.util.* ;
import java.util.stream.* ;
import java.text.* ;
import java.math.* ;
public class Codeshefcode{
	public static void main(String[] args) throws IOException{
		Solver Machine = new Solver() ;
		Machine.Solve() ;	
		Machine.Finish();
	}	
}
class Mod{
	static long mod=1000000007 ;
	static long d(long a,long b){ return (a*MI(b))%mod ; }
	static long m(long a , long b){ return (a*b)%mod ; }
	static private long MI(long a ){ return pow(a,mod-2) ; }
	static long pow(long a,long b){
		if(b<0) return pow(MI(a),-b) ;
		long val=a ; long ans=1 ;
		while(b!=0){
			if((b&1)==1) ans = (ans*val)%mod ; 
			val = (val*val)%mod ; 
			b>>=1 ;
		}
		return ans ;
	}	
}
class pair implements Comparable<pair>{
	long x ; long y ; 
	pair(long x,long y){ this.x=x ; this.y=y ;}
	public int compareTo(pair p){
		return (this.x<p.x ? -1 : (this.x>p.x ? 1 : (this.y<p.y ? -1 : (this.y>p.y ? 1 : 0)))) ;
	}
}
class Solver{	
	FasterScanner ip = new FasterScanner(System.in) ;	  
	PrintWriter op = new PrintWriter(System.out) ;
	void Solve() throws IOException{
		int n = ip.i() ; int S = ip.i() ;
		long s[] = new long[n] ;
		long a[] = new long[n] ;
		long b[] = new long[n] ;
		for(int i=0 ; i<n ; i++){
			s[i] = ip.i() ;
			a[i] = ip.i() ;
			b[i] = ip.i() ;
		}
		long tot=0 ;
		for(long elem : s) tot+=elem ;
		long pizz = tot/S+((tot%S==0) ? 0 : 1) ;
		long hap=0 ;
		for(int i=0 ; i<n ; i++) hap+=(s[i]*b[i]) ;
		for(int i=0 ; i<n ; i++) a[i]-=b[i] ;
		pair data[] = new pair[n] ;
		for(int i=0 ; i<n ; i++) data[i] = new pair(a[i],s[i]) ;
		Arrays.sort(data) ;
		for(int i=0 ; i<(n-i) ; i++){
			pair aux = data[i] ;
			data[i] = data[n-(i+1)] ;
			data[n-(i+1)]=aux ;
		}
		long pca=0 ;
		long pcb=0 ;
		int ptr=0 ;
		for(ptr=0 ; ptr<n ; ptr++) 
			if(data[ptr].x>=0){ 
				pca+=data[ptr].y ;
				hap+=(data[ptr].x*data[ptr].y) ;
			} 
			else 
				break ;
		pcb = tot-pca ;
		long taken=0 ;
		int baseptr=ptr ;
		long basehap=hap ;
		long basepca=pca ;
		//p("hap="+hap+",") ;
		//p("pca="+pca+",") ;
		//pln("pcb="+pcb) ;
		while(true){
			long pizza = pca/S+((pca%S==0) ? 0 : 1) ;
			long pizzb = pcb/S+((pcb%S==0) ? 0 : 1) ;
			if(pizza+pizzb==pizz) break ;
			pca++ ; pcb=(tot-pca) ;
			taken++ ;
			hap+=data[ptr].x ;
			if(taken==data[ptr].y){
				taken=0 ;
				ptr++ ;
			}
		}
		//p("hap="+hap+",") ; 
		//p("pca="+pca+",") ;
		//pln("pcb="+pcb) ;
		long res = hap ;
		hap=basehap ;
		ptr=baseptr ;
		pca=basepca ;
		pcb=tot-pca ;
		taken=0 ;
		while(true){
			long pizza = pca/S+((pca%S==0) ? 0 : 1) ;
			long pizzb = pcb/S+((pcb%S==0) ? 0 : 1) ;
			if(pizza+pizzb==pizz) break ;
			if(taken==0){
				ptr-- ;
				taken = data[ptr].y ;
			}
			taken-- ;
			pca-- ; pcb=(tot-pca) ;
			hap-=data[ptr].x ;
		}
		//p("hap="+hap+",") ;
		//p("pca="+pca+",") ;
		//pln("pcb="+pcb) ;
		res = Long.max(res,hap) ;
		pln(res) ;
	}
	void p(Object o){
		op.print(o) ;
	}
	void pln(Object o){
		op.println(o) ;
	}
	void pArr(String name,Object arr,char type){
		p(name+": [") ;
		if(type=='i'){ 
			int arr1[] = (int[]) arr ;
			for(int i=0 ; i<arr1.length ; i++) p(arr1[i]+(i+1==arr1.length ? "" : ",")) ;		 
		}else if(type=='l'){ 
			long arr1[] = (long[]) arr ;
			for(int i=0 ; i<arr1.length ; i++) p(arr1[i]+(i+1==arr1.length ? "" : ",")) ;
		}else if(type=='b'){
			boolean arr1[] = (boolean[]) arr ;
			for(int i=0 ; i<arr1.length ; i++) p((arr1[i] ? 1 : 0)+(i+1==arr1.length ? "" : ",")) ;
		}
		pln("]") ;
	}	
	void Finish(){
		op.flush();
		op.close();
	}	
}
@SuppressWarnings("serial")
class mylist extends ArrayList<Integer>{
}
@SuppressWarnings("serial")
class myset extends TreeSet<Long>{
 
}
@SuppressWarnings("serial")
class mystack extends Stack<Integer>{
}
@SuppressWarnings("serial")
class mymap extends TreeMap<Long,Integer>{
}
class FasterScanner { 
	private InputStream mIs;
	private byte[] buf = new byte[1024];
	private int curChar,numChars;
	public FasterScanner() { this(System.in); } 
	public FasterScanner(InputStream is) { mIs = is;} 
	public int read() {
		if (numChars == -1) throw new InputMismatchException();    
		if (curChar >= numChars) {
			curChar = 0;
			try { numChars = mIs.read(buf);} catch (IOException e) { throw new InputMismatchException();}
			if (numChars <= 0) return -1;            
		}
		return buf[curChar++];
	} 
	public String nextLine(){
		int c = read();
		while (isSpaceChar(c)) c = read();        
		StringBuilder res = new StringBuilder();
		do {
			res.appendCodePoint(c);
			c = read();
		} while (!isEndOfLine(c));
		return res.toString();
	} 
	public String S(){
		int c = read();
		while (isSpaceChar(c)) c = read();        
		StringBuilder res = new StringBuilder();
		do {
			res.appendCodePoint(c);
			c = read();
		}while (!isSpaceChar(c));
		return res.toString();
	} 
	public long l(){
		int c = read();
		while (isSpaceChar(c)) c = read();        
		int sgn = 1;
		if (c == '-') { sgn = -1 ; c = read() ; }
		long res = 0;
		do{
			if (c < '0' || c > '9') throw new InputMismatchException();
			res *= 10 ; res += c - '0' ; c = read();
		}while(!isSpaceChar(c));
		return res * sgn;
	} 
	public int i(){
		int c = read() ;
		while (isSpaceChar(c)) c = read();        
		int sgn = 1;
		if (c == '-') { sgn = -1 ; c = read() ; }
		int res = 0;
		do{
			if (c < '0' || c > '9') throw new InputMismatchException();
		res *= 10 ; res += c - '0' ; c = read() ;
		}while(!isSpaceChar(c));
		return res * sgn;
	} 
	public int[] ia(int n){
		int a[] = new int[n] ;
		for(int i=0 ; i<n ; i++) a[i] = i() ;
		return a ;
	}
	public long[] la(int n){
		long a[] = new long[n] ;
		for(int i=0 ; i<n ; i++) a[i] = l() ;
		return a ;
	}
	public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } 
	public boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } 
} 