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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.util.*;

public class QmemC {	
	BufferedReader reader;
    StringTokenizer tokenizer;
    PrintWriter out;
    
	public void solve() throws IOException {				
		int N = nextInt();
    long S = nextInt();

    long[][] input = new long[N][];
    long sum = 0;
    for (int i = 0; i < N; i++) {
      int s = nextInt();
      int a = nextInt();
      int b = nextInt();
      input[i] = new long[] {s, a, b};
      sum += s;
    }

    Comparator<long[]> cmp = new Comparator<long[]> () {
      @Override
      public int compare(long[] a, long[] b) {
        return (int)(b[1]-b[2]) - (int)(a[1]-a[2]);
      }
    };
    Arrays.sort(input, cmp);


    long nPizza = (sum-1)/ S + 1;
    long ans = 0;

    long left = 0;
    long right = nPizza;
    while (right - left > 5) {
      long l1 = (left * 2 + right) / 3;
      long l2 = (right * 2 + left) / 3;
      long v1 = compute(N, S, input, l1, nPizza - l1);
      long v2 = compute(N, S, input, l2, nPizza - l2);
      if (v1 < v2) {
        left = l1;
      } else {
        right = l2;
      }
    }

    for (long i = left; i <= right; i++) {
      //out.println(i + ", " + (nPizza-i) + ": " + compute(N, S, input, i, nPizza-i));
      ans = Math.max(ans, compute(N, S, input, i, nPizza-i)); 
    }
    out.println(ans);
  }

  public long compute(int N, long S, long[][] input, long p1, long p2) {
    long[] sumRest = new long[N];
    for (int i = N-1; i >= 0; i--) {
      sumRest[i] = input[i][0] +  (i < N-1? sumRest[i+1]: 0);
    }

    long ans = 0;
    int pizzaIndex = p1 > 0? 1: 2;
    p1--;
    long remain = S;
    for (int i = 0; i < N; i++) {
      long need = input[i][0];

      if (pizzaIndex == 1) {
        if (input[i][1] > input[i][2]) {
          if (need <= remain) {
            ans += need * input[i][1];
            remain -= need;
            continue;
          }
          ans += remain * input[i][1];
          need -= remain;

          long full = Math.min(need / S, p1);
          p1 -= full;

          ans += S * full * input[i][1];
          long rest = need - full * S;

          if (p1 > 0) {
            p1--;
            ans += rest * input[i][1];
            remain = S - rest;
          } else {
            pizzaIndex = 2;
            ans += rest * input[i][2];
          }
        } else {
          long maxP2 = p2 * S;
          long requiredP1 = sumRest[i] - maxP2;

          if (requiredP1 <= 0) {
            pizzaIndex = 2;
            ans += input[i][0] * input[i][2];
          } else {
            long eat1 = Math.min(need, requiredP1);
            long eat2 = input[i][0] - eat1;
            ans += eat1 * input[i][1];
            //out.println("e1: " + eat1 + ", " + eat2 + ", " + need + ", " + requiredP1 + " = " + ans);

            if (eat2 > 0) {
              pizzaIndex = 2;
              ans += eat2 * input[i][2];
            }
          }
        }
      } 
      else {
        ans += input[i][0] * input[i][pizzaIndex];
      }
    }
    return ans;
  }
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new QmemC().run();
	}
	
	public void run() {
        try {
            reader = new BufferedReader(new InputStreamReader(System.in));
            tokenizer = null;
            out = new PrintWriter(System.out);
            solve();
            reader.close();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }

    long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }

    double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }

    String nextToken() throws IOException {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            tokenizer = new StringTokenizer(reader.readLine());
        }
        return tokenizer.nextToken();
    }

}

