import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.SortedSet;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;

/**
 * #
 * @author pttrung
 */
public class B_Round_437_Div1 {

    public static long MOD = 1000000007;

    public static void main(String[] args) throws FileNotFoundException {
        // PrintWriter out = new PrintWriter(new FileOutputStream(new File(
        // "output.txt")));
        PrintWriter out = new PrintWriter(System.out);
        Scanner in = new Scanner();
        int n = in.nextInt();
        long s = in.nextInt();
        final long[][] data = new long[n][3];
        int[] p = new int[n];
        int[] q = new int[n];
        PriorityQueue<Integer> P = new PriorityQueue<>((x, y) -> Long.compare(data[y][1], data[x][1]));
        PriorityQueue<Integer> Q = new PriorityQueue<>((x, y) -> Long.compare(data[y][2], data[x][2]));
        long total = 0;
        for (int i = 0; i < n; i++) {

            for (int j = 0; j < 3; j++) {
                data[i][j] = in.nextInt();

            }
            total += data[i][0];
            P.add(i);
            Q.add(i);
        }
        for (int i = 0; i < n; i++) {
            p[i] = P.poll();
            q[i] = Q.poll();
        }

        long need = total / s;
        if (need * s < total) {
            need++;
        }

        long st = 0;
        long ed = need;
        long re = 0;
        while (st < ed) {
            long mid = (st + ed) / 2;
            long x = cal(data, p, q, s * mid, s * (need - mid), n);
            long y = cal(data, p, q, s * (mid + 1), s * (need - mid - 1), n);
            //System.out.println(x + " " + y + " " + mid);
            if (x >= y) {
                ed = mid;
                re = Long.max(x, re);
            } else {
                st = mid + 1;
                re = Long.max(y, re);
            }
        }
        out.println(re);
        out.close();
    }

    static long cal(long[][] data, int[] p, int[] q, long a, long b, int n) {
        //System.out.println("Start " + a + " " + b);
        long re = 0;
        long[] hold = new long[n];

        int i = 0, j = 0;
        while (i < n && j < n) {
            if (data[p[i]][1] >= data[q[j]][2]) {
                int v = p[i++];
                long need = data[v][0] - hold[v];
                need = Long.min(need, a);
                a -= need;
                hold[v] += need;
                re += data[v][1] * need;
            } else {
                int v = q[j++];
                long need = data[v][0] - hold[v];
                need = Long.min(need, b);
                b -= need;
                hold[v] += need;
                re += data[v][2] * need;
            }
        }
        while (i < n) {
            int v = p[i++];
            long need = data[v][0] - hold[v];
            need = Long.min(need, a);
            a -= need;
            hold[v] += need;
            re += data[v][1] * need;
        }
        while (j < n) {
            int v = q[j++];
            long need = data[v][0] - hold[v];
            need = Long.min(need, b);
            b -= need;
            hold[v] += need;
            re += data[v][2] * need;
        }

        return re;
    }

    public static int[] KMP(String val) {
        int i = 0;
        int j = -1;
        int[] result = new int[val.length() + 1];
        result[0] = -1;
        while (i < val.length()) {
            while (j >= 0 && val.charAt(j) != val.charAt(i)) {
                j = result[j];
            }
            j++;
            i++;
            result[i] = j;
        }
        return result;

    }

    public static boolean nextPer(int[] data) {
        int i = data.length - 1;
        while (i > 0 && data[i] < data[i - 1]) {
            i--;
        }
        if (i == 0) {
            return false;
        }
        int j = data.length - 1;
        while (data[j] < data[i - 1]) {
            j--;
        }
        int temp = data[i - 1];
        data[i - 1] = data[j];
        data[j] = temp;
        Arrays.sort(data, i, data.length);
        return true;
    }

    public static int digit(long n) {
        int result = 0;
        while (n > 0) {
            n /= 10;
            result++;
        }
        return result;
    }

    public static double dist(long a, long b, long x, long y) {
        double val = (b - a) * (b - a) + (x - y) * (x - y);
        val = Math.sqrt(val);
        double other = x * x + a * a;
        other = Math.sqrt(other);
        return val + other;

    }

    public static class Point implements Comparable<Point> {

        int x, y;

        public Point(int start, int end) {
            this.x = start;
            this.y = end;
        }

        @Override
        public int hashCode() {
            int hash = 5;
            hash = 47 * hash + this.x;
            hash = 47 * hash + this.y;
            return hash;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final Point other = (Point) obj;
            if (this.x != other.x) {
                return false;
            }
            if (this.y != other.y) {
                return false;
            }
            return true;
        }

        @Override
        public int compareTo(Point o) {
            return Integer.compare(x, o.x);
        }
    }

    public static class FT {

        long[] data;

        FT(int n) {
            data = new long[n];
        }

        public void update(int index, long value) {
            while (index < data.length) {
                data[index] += value;
                index += (index & (-index));
            }
        }

        public long get(int index) {
            long result = 0;
            while (index > 0) {
                result += data[index];
                index -= (index & (-index));
            }
            return result;

        }
    }

    public static long gcd(long a, long b) {
        if (b == 0) {
            return a;
        }
        return gcd(b, a % b);
    }

    public static long pow(long a, long b, long MOD) {
        if (b == 0) {
            return 1;
        }
        if (b == 1) {
            return a;
        }
        long val = pow(a, b / 2, MOD);
        if (b % 2 == 0) {
            return val * val % MOD;
        } else {
            return val * (val * a % MOD) % MOD;

        }
    }

    static class Scanner {

        BufferedReader br;
        StringTokenizer st;

        public Scanner() throws FileNotFoundException {
            // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true));
            br = new BufferedReader(new InputStreamReader(System.in));
            //  br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
        }

        public String next() {

            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (Exception e) {
                    throw new RuntimeException();
                }
            }
            return st.nextToken();
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public String nextLine() {
            st = null;
            try {
                return br.readLine();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }

        public boolean endLine() {
            try {
                String next = br.readLine();
                while (next != null && next.trim().isEmpty()) {
                    next = br.readLine();
                }
                if (next == null) {
                    return true;
                }
                st = new StringTokenizer(next);
                return st.hasMoreTokens();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }
    }
}


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.SortedSet;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;

/**
 * #
 * @author pttrung
 */
public class B_Round_437_Div1 {

    public static long MOD = 1000000007;

    public static void main(String[] args) throws FileNotFoundException {
        // PrintWriter out = new PrintWriter(new FileOutputStream(new File(
        // "output.txt")));
        PrintWriter out = new PrintWriter(System.out);
        Scanner in = new Scanner();
        int n = in.nextInt();
        long s = in.nextInt();
        long[][] data = new long[n][4];
        long[][] other = new long[n][4];
        long total = 0;
        for (int i = 0; i < n; i++) {
            data[i][3] = i;
            other[i][3] = i;
            for (int j = 0; j < 3; j++) {
                data[i][j] = in.nextInt();
                other[i][j] = data[i][j];
            }
            total += data[i][0];
        }
        long need = total / s;
        if (need * s < total) {
            need++;
        }
        //System.out.println(need);
        Arrays.sort(data, (x, y) -> {

            return Long.compare(y[1], x[1]);

        });
        Arrays.sort(other, (x, y) -> {

            return Long.compare(y[2], x[2]);

        });
        long st = 0;
        long ed = need;
        long re = 0;
        while (st < ed) {
            long mid = (st + ed) / 2;
            long x = cal(data, other, s * mid, s * (need - mid));
            long y = cal(data, other, s * (mid + 1), s * (need - mid - 1));
            //System.out.println(x + " " + y + " " + mid);
            if (x > y) {
                ed = mid;
                re = Long.max(x, re);
            } else {
                st = mid + 1;
                re = Long.max(y, re);
            }
        }
        out.println(re);
        out.close();
    }

    static long cal(long[][] data, long[][] other, long a, long b) {
        //System.out.println("Start " + a + " " + b);
        long re = 0;
        boolean[] check = new boolean[data.length];
        int i = 0;
        int j = 0;
        while ((i < data.length && data[i][1] >= data[i][2] && a > 0) || (j < other.length && other[j][2] >= other[j][1] && b > 0)) {
            if (i < data.length && data[i][1] >= data[i][2] && a > 0 && (j == other.length || other[j][2] <= data[i][1] || b < 0)) {
                if (check[(int)data[i][3]]) {
                    i++;
                    continue;
                }
                check[(int)data[i][3]] = true;
                //System.out.println(Arrays.toString(data[i]) + " " + a + " " + b + " First");
                long v = Long.min(a, data[i][0]);
                a -= v;

                re += data[i][1] * v;
                if (v < data[i][0]) {
                    b -= (data[i][0] - v);
                    re += (data[i][0] - v) * data[i][2];
                }

                i++;
            }else if(j < other.length && other[j][2] >= other[j][1] && b > 0) {
                if (check[(int)other[j][3]]) {
                    j++;
                    continue;
                }
                //System.out.println(Arrays.toString(other[j]) + " " + a + " " + b + " Second");
                check[(int)other[j][3]] = true;
                long v = Long.min(b, other[j][0]);
                b -= v;
                re += other[j][2] * v;
                if (v < other[j][0]) {
                    a -= (other[j][0] - v);
                    re += (other[j][0] - v) * other[j][1];
                }
                j++;
            }
            while (i < data.length && check[(int)data[i][3]]) {
                i++;
            }
            while (j < other.length && check[(int)other[j][3]]) {
                j++;
            }
        }
        while (i < data.length && a > 0) {
            if (check[(int)data[i][3]]) {
                i++;
                continue;
            }
            //System.out.println(Arrays.toString(data[i]) + " " + a + " " + b + " Third");
            long v = Long.min(a, data[i][0]);
            a -= v;
            re += data[i][1] * v;
            if (v < data[i][0]) {
                b -= (data[i][0] - v);
                re += (data[i][0] - v) * data[i][2];
            }
            i++;
        }
        while (j < other.length && b > 0) {
            if (check[(int)other[j][3]]) {
                j++;
                continue;
            }
            // System.out.println(Arrays.toString(other[j]) + " " + a + " " + b + " Fourth");
            check[(int)other[j][3]] = true;
            long v = Long.min(b, other[j][0]);
            b -= v;
            re += other[j][2] * v;
            if (v < other[j][0]) {
                if (a < (other[j][0] - v)) {
                    throw new NullPointerException();
                }
                a -= (other[j][0] - v);
                re += (other[j][0] - v) * other[j][1];
            }
            j++;
        }
        return re;
    }

    public static int[] KMP(String val) {
        int i = 0;
        int j = -1;
        int[] result = new int[val.length() + 1];
        result[0] = -1;
        while (i < val.length()) {
            while (j >= 0 && val.charAt(j) != val.charAt(i)) {
                j = result[j];
            }
            j++;
            i++;
            result[i] = j;
        }
        return result;

    }

    public static boolean nextPer(int[] data) {
        int i = data.length - 1;
        while (i > 0 && data[i] < data[i - 1]) {
            i--;
        }
        if (i == 0) {
            return false;
        }
        int j = data.length - 1;
        while (data[j] < data[i - 1]) {
            j--;
        }
        int temp = data[i - 1];
        data[i - 1] = data[j];
        data[j] = temp;
        Arrays.sort(data, i, data.length);
        return true;
    }

    public static int digit(long n) {
        int result = 0;
        while (n > 0) {
            n /= 10;
            result++;
        }
        return result;
    }

    public static double dist(long a, long b, long x, long y) {
        double val = (b - a) * (b - a) + (x - y) * (x - y);
        val = Math.sqrt(val);
        double other = x * x + a * a;
        other = Math.sqrt(other);
        return val + other;

    }

    public static class Point implements Comparable<Point> {

        int x, y;

        public Point(int start, int end) {
            this.x = start;
            this.y = end;
        }

        @Override
        public int hashCode() {
            int hash = 5;
            hash = 47 * hash + this.x;
            hash = 47 * hash + this.y;
            return hash;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final Point other = (Point) obj;
            if (this.x != other.x) {
                return false;
            }
            if (this.y != other.y) {
                return false;
            }
            return true;
        }

        @Override
        public int compareTo(Point o) {
            return Integer.compare(x, o.x);
        }
    }

    public static class FT {

        long[] data;

        FT(int n) {
            data = new long[n];
        }

        public void update(int index, long value) {
            while (index < data.length) {
                data[index] += value;
                index += (index & (-index));
            }
        }

        public long get(int index) {
            long result = 0;
            while (index > 0) {
                result += data[index];
                index -= (index & (-index));
            }
            return result;

        }
    }

    public static long gcd(long a, long b) {
        if (b == 0) {
            return a;
        }
        return gcd(b, a % b);
    }

    public static long pow(long a, long b, long MOD) {
        if (b == 0) {
            return 1;
        }
        if (b == 1) {
            return a;
        }
        long val = pow(a, b / 2, MOD);
        if (b % 2 == 0) {
            return val * val % MOD;
        } else {
            return val * (val * a % MOD) % MOD;

        }
    }

    static class Scanner {

        BufferedReader br;
        StringTokenizer st;

        public Scanner() throws FileNotFoundException {
            // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true));
            br = new BufferedReader(new InputStreamReader(System.in));
            //  br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
        }

        public String next() {

            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (Exception e) {
                    throw new RuntimeException();
                }
            }
            return st.nextToken();
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public String nextLine() {
            st = null;
            try {
                return br.readLine();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }

        public boolean endLine() {
            try {
                String next = br.readLine();
                while (next != null && next.trim().isEmpty()) {
                    next = br.readLine();
                }
                if (next == null) {
                    return true;
                }
                st = new StringTokenizer(next);
                return st.hasMoreTokens();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }
    }
}


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.SortedSet;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;

/**
 * #
 * @author pttrung
 */
public class B_Round_437_Div1 {

    public static long MOD = 1000000007;

    public static void main(String[] args) throws FileNotFoundException {
        // PrintWriter out = new PrintWriter(new FileOutputStream(new File(
        // "output.txt")));
        PrintWriter out = new PrintWriter(System.out);
        Scanner in = new Scanner();
        int n = in.nextInt();
        long s = in.nextInt();
        final long[][] data = new long[n][3];
        int[] p = new int[n];
        int[] q = new int[n];
        PriorityQueue<Integer> P = new PriorityQueue<>((x, y) -> Long.compare(data[y][1] - data[y][2], data[x][1] - data[x][2]));
        PriorityQueue<Integer> Q = new PriorityQueue<>((x, y) -> Long.compare(data[y][2] - data[y][1], data[x][2] - data[x][1]));
        long total = 0;
        for (int i = 0; i < n; i++) {

            for (int j = 0; j < 3; j++) {
                data[i][j] = in.nextInt();

            }
            total += data[i][0];
            P.add(i);
            Q.add(i);
        }
        for (int i = 0; i < n; i++) {
            p[i] = P.poll();
            q[i] = Q.poll();
        }

        long need = total / s;
        if (need * s < total) {
            need++;
        }

        long st = 0;
        long ed = need;
        long re = 0;
        while (st < ed) {
            long mid = (st + ed) / 2;
            long x = cal(data, p, q, s * mid, s * (need - mid), n);
            long y = cal(data, p, q, s * (mid + 1), s * (need - mid - 1), n);
            //System.out.println(x + " " + y + " " + mid);
            if (x >= y) {
                ed = mid;
                re = Long.max(x, re);
            } else {
                st = mid + 1;
                re = Long.max(y, re);
            }
        }
        out.println(re);
        out.close();
    }

    static long cal(long[][] data, int[] p, int[] q, long a, long b, int n) {
        //System.out.println("Start " + a + " " + b);
        long re = 0;
        long[] hold = new long[n];

        int i = 0, j = 0;
        while (i < n && j < n) {
            if (data[p[i]][1] - data[p[i]][2] >= data[q[j]][2] - data[q[j]][1]) {
                int v = p[i++];
                long need = data[v][0] - hold[v];
                need = Long.min(need, a);
                a -= need;
                hold[v] += need;
                re += data[v][1] * need;
            } else {
                int v = q[j++];
                long need = data[v][0] - hold[v];
                need = Long.min(need, b);
                b -= need;
                hold[v] += need;
                re += data[v][2] * need;
            }
        }
        while (i < n) {
            int v = p[i++];
            long need = data[v][0] - hold[v];
            need = Long.min(need, a);
            a -= need;
            hold[v] += need;
            re += data[v][1] * need;
        }
        while (j < n) {
            int v = q[j++];
            long need = data[v][0] - hold[v];
            need = Long.min(need, b);
            b -= need;
            hold[v] += need;
            re += data[v][2] * need;
        }

        return re;
    }

    public static int[] KMP(String val) {
        int i = 0;
        int j = -1;
        int[] result = new int[val.length() + 1];
        result[0] = -1;
        while (i < val.length()) {
            while (j >= 0 && val.charAt(j) != val.charAt(i)) {
                j = result[j];
            }
            j++;
            i++;
            result[i] = j;
        }
        return result;

    }

    public static boolean nextPer(int[] data) {
        int i = data.length - 1;
        while (i > 0 && data[i] < data[i - 1]) {
            i--;
        }
        if (i == 0) {
            return false;
        }
        int j = data.length - 1;
        while (data[j] < data[i - 1]) {
            j--;
        }
        int temp = data[i - 1];
        data[i - 1] = data[j];
        data[j] = temp;
        Arrays.sort(data, i, data.length);
        return true;
    }

    public static int digit(long n) {
        int result = 0;
        while (n > 0) {
            n /= 10;
            result++;
        }
        return result;
    }

    public static double dist(long a, long b, long x, long y) {
        double val = (b - a) * (b - a) + (x - y) * (x - y);
        val = Math.sqrt(val);
        double other = x * x + a * a;
        other = Math.sqrt(other);
        return val + other;

    }

    public static class Point implements Comparable<Point> {

        int x, y;

        public Point(int start, int end) {
            this.x = start;
            this.y = end;
        }

        @Override
        public int hashCode() {
            int hash = 5;
            hash = 47 * hash + this.x;
            hash = 47 * hash + this.y;
            return hash;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final Point other = (Point) obj;
            if (this.x != other.x) {
                return false;
            }
            if (this.y != other.y) {
                return false;
            }
            return true;
        }

        @Override
        public int compareTo(Point o) {
            return Integer.compare(x, o.x);
        }
    }

    public static class FT {

        long[] data;

        FT(int n) {
            data = new long[n];
        }

        public void update(int index, long value) {
            while (index < data.length) {
                data[index] += value;
                index += (index & (-index));
            }
        }

        public long get(int index) {
            long result = 0;
            while (index > 0) {
                result += data[index];
                index -= (index & (-index));
            }
            return result;

        }
    }

    public static long gcd(long a, long b) {
        if (b == 0) {
            return a;
        }
        return gcd(b, a % b);
    }

    public static long pow(long a, long b, long MOD) {
        if (b == 0) {
            return 1;
        }
        if (b == 1) {
            return a;
        }
        long val = pow(a, b / 2, MOD);
        if (b % 2 == 0) {
            return val * val % MOD;
        } else {
            return val * (val * a % MOD) % MOD;

        }
    }

    static class Scanner {

        BufferedReader br;
        StringTokenizer st;

        public Scanner() throws FileNotFoundException {
            // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true));
            br = new BufferedReader(new InputStreamReader(System.in));
            //  br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
        }

        public String next() {

            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (Exception e) {
                    throw new RuntimeException();
                }
            }
            return st.nextToken();
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public String nextLine() {
            st = null;
            try {
                return br.readLine();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }

        public boolean endLine() {
            try {
                String next = br.readLine();
                while (next != null && next.trim().isEmpty()) {
                    next = br.readLine();
                }
                if (next == null) {
                    return true;
                }
                st = new StringTokenizer(next);
                return st.hasMoreTokens();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }
    }
}


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.SortedSet;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;

/**
 * #
 * @author pttrung
 */
public class B_Round_437_Div1 {

    public static long MOD = 1000000007;

    public static void main(String[] args) throws FileNotFoundException {
        // PrintWriter out = new PrintWriter(new FileOutputStream(new File(
        // "output.txt")));
        PrintWriter out = new PrintWriter(System.out);
        Scanner in = new Scanner();
        int n = in.nextInt();
        int s = in.nextInt();
        int[][] data = new int[n][4];
        int[][] other = new int[n][4];
        int total = 0;
        for (int i = 0; i < n; i++) {
            data[i][3] = i;
            other[i][3] = i;
            for (int j = 0; j < 3; j++) {
                data[i][j] = in.nextInt();
                other[i][j] = data[i][j];
            }
            total += data[i][0];
        }
        int need = total / s;
        if (need * s < total) {
            need++;
        }
        //System.out.println(need);
        Arrays.sort(data, (x, y) -> {

            return Integer.compare(y[1], x[1]);

        });
        Arrays.sort(other, (x, y) -> {

            return Integer.compare(y[2], x[2]);

        });
        int st = 0;
        int ed = need;
        int re = 0;
        while (st < ed) {
            int mid = (st + ed) / 2;
            int x = cal(data, other, s * mid, s * (need - mid));
            int y = cal(data, other, s * (mid + 1), s * (need - mid - 1));
            //System.out.println(x + " " + y + " " + mid);
            if (x > y) {
                ed = mid;
                re = Integer.max(x, re);
            } else {
                st = mid + 1;
                re = Integer.max(y, re);
            }
        }
        out.println(re);
        out.close();
    }

    static int cal(int[][] data, int[][] other, int a, int b) {
        //System.out.println("Start " + a + " " + b);
        int re = 0;
        boolean[] check = new boolean[data.length];
        int i = 0;
        int j = 0;
        while ((i < data.length && data[i][1] >= data[i][2] && a > 0) || (j < other.length && other[j][2] >= other[j][1] && b > 0)) {
            while (i < data.length && data[i][1] >= data[i][2] && a > 0) {
                if (check[data[i][3]]) {
                    i++;
                    continue;
                }
                check[data[i][3]] = true;
                //System.out.println(Arrays.toString(data[i]) + " " + a + " " + b + " First");
                int v = Integer.min(a, data[i][0]);
                a -= v;

                re += data[i][1] * v;
                if (v < data[i][0]) {
                    b -= (data[i][0] - v);
                    re += (data[i][0] - v) * data[i][2];
                }

                i++;
            }
            while (j < other.length && other[j][2] >= other[j][1] && b > 0) {
                if (check[other[j][3]]) {
                    j++;
                    continue;
                }
                //System.out.println(Arrays.toString(other[j]) + " " + a + " " + b + " Second");
                check[other[j][3]] = true;
                int v = Integer.min(b, other[j][0]);
                b -= v;
                re += other[j][2] * v;
                if (v < other[j][0]) {
                    a -= (other[j][0] - v);
                    re += (other[j][0] - v) * other[j][1];
                }
                j++;
            }
            while (i < data.length && check[data[i][3]]) {
                i++;
            }
            while (j < other.length && check[other[j][3]]) {
                j++;
            }
        }
        while (i < data.length && a > 0) {
            if (check[data[i][3]]) {
                i++;
                continue;
            }
            //System.out.println(Arrays.toString(data[i]) + " " + a + " " + b + " Third");
            int v = Integer.min(a, data[i][0]);
            a -= v;
            re += data[i][1] * v;
            if (v < data[i][0]) {
                b -= (data[i][0] - v);
                re += (data[i][0] - v) * data[i][2];
            }
            i++;
        }
        while (j < other.length && b > 0) {
            if (check[other[j][3]]) {
                j++;
                continue;
            }
            // System.out.println(Arrays.toString(other[j]) + " " + a + " " + b + " Fourth");
            check[other[j][3]] = true;
            int v = Integer.min(b, other[j][0]);
            b -= v;
            re += other[j][2] * v;
            if (v < other[j][0]) {
                if (a < (other[j][0] - v)) {
                    throw new NullPointerException();
                }
                a -= (other[j][0] - v);
                re += (other[j][0] - v) * other[j][1];
            }
            j++;
        }
        return re;
    }

    public static int[] KMP(String val) {
        int i = 0;
        int j = -1;
        int[] result = new int[val.length() + 1];
        result[0] = -1;
        while (i < val.length()) {
            while (j >= 0 && val.charAt(j) != val.charAt(i)) {
                j = result[j];
            }
            j++;
            i++;
            result[i] = j;
        }
        return result;

    }

    public static boolean nextPer(int[] data) {
        int i = data.length - 1;
        while (i > 0 && data[i] < data[i - 1]) {
            i--;
        }
        if (i == 0) {
            return false;
        }
        int j = data.length - 1;
        while (data[j] < data[i - 1]) {
            j--;
        }
        int temp = data[i - 1];
        data[i - 1] = data[j];
        data[j] = temp;
        Arrays.sort(data, i, data.length);
        return true;
    }

    public static int digit(long n) {
        int result = 0;
        while (n > 0) {
            n /= 10;
            result++;
        }
        return result;
    }

    public static double dist(long a, long b, long x, long y) {
        double val = (b - a) * (b - a) + (x - y) * (x - y);
        val = Math.sqrt(val);
        double other = x * x + a * a;
        other = Math.sqrt(other);
        return val + other;

    }

    public static class Point implements Comparable<Point> {

        int x, y;

        public Point(int start, int end) {
            this.x = start;
            this.y = end;
        }

        @Override
        public int hashCode() {
            int hash = 5;
            hash = 47 * hash + this.x;
            hash = 47 * hash + this.y;
            return hash;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final Point other = (Point) obj;
            if (this.x != other.x) {
                return false;
            }
            if (this.y != other.y) {
                return false;
            }
            return true;
        }

        @Override
        public int compareTo(Point o) {
            return Integer.compare(x, o.x);
        }
    }

    public static class FT {

        long[] data;

        FT(int n) {
            data = new long[n];
        }

        public void update(int index, long value) {
            while (index < data.length) {
                data[index] += value;
                index += (index & (-index));
            }
        }

        public long get(int index) {
            long result = 0;
            while (index > 0) {
                result += data[index];
                index -= (index & (-index));
            }
            return result;

        }
    }

    public static long gcd(long a, long b) {
        if (b == 0) {
            return a;
        }
        return gcd(b, a % b);
    }

    public static long pow(long a, long b, long MOD) {
        if (b == 0) {
            return 1;
        }
        if (b == 1) {
            return a;
        }
        long val = pow(a, b / 2, MOD);
        if (b % 2 == 0) {
            return val * val % MOD;
        } else {
            return val * (val * a % MOD) % MOD;

        }
    }

    static class Scanner {

        BufferedReader br;
        StringTokenizer st;

        public Scanner() throws FileNotFoundException {
            // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true));
            br = new BufferedReader(new InputStreamReader(System.in));
            //  br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
        }

        public String next() {

            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (Exception e) {
                    throw new RuntimeException();
                }
            }
            return st.nextToken();
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public String nextLine() {
            st = null;
            try {
                return br.readLine();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }

        public boolean endLine() {
            try {
                String next = br.readLine();
                while (next != null && next.trim().isEmpty()) {
                    next = br.readLine();
                }
                if (next == null) {
                    return true;
                }
                st = new StringTokenizer(next);
                return st.hasMoreTokens();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }
    }
}


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.SortedSet;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;

/**
 * #
 * @author pttrung
 */
public class B_Round_437_Div1 {

    public static long MOD = 1000000007;

    public static void main(String[] args) throws FileNotFoundException {
        // PrintWriter out = new PrintWriter(new FileOutputStream(new File(
        // "output.txt")));
        PrintWriter out = new PrintWriter(System.out);
        Scanner in = new Scanner();
        int n = in.nextInt();
        long s = in.nextInt();
        long[][] data = new long[n][4];
        long total = 0;
        for (int i = 0; i < n; i++) {
            data[i][3] = i;

            for (int j = 0; j < 3; j++) {
                data[i][j] = in.nextInt();

            }
            total += data[i][0];
        }
        long need = total / s;
        if (need * s < total) {
            need++;
        }

        long st = 0;
        long ed = need;
        long re = 0;
        while (st < ed) {
            long mid = (st + ed) / 2;
            long x = cal(data, s * mid, s * (need - mid));
            long y = cal(data, s * (mid + 1), s * (need - mid - 1));
            //System.out.println(x + " " + y + " " + mid);
            if (x > y) {
                ed = mid;
                re = Long.max(x, re);
            } else {
                st = mid + 1;
                re = Long.max(y, re);
            }
        }
        out.println(re);
        out.close();
    }

    static long cal(final long[][] data, long a, long b) {
        //System.out.println("Start " + a + " " + b);
        long re = 0;
        boolean[] check = new boolean[data.length];
        long[] hold = new long[data.length];
        PriorityQueue<Integer> p = new PriorityQueue<>((x, y) -> Long.compare(data[y][1], data[x][1]));
        PriorityQueue<Integer> q = new PriorityQueue<>((x, y) -> Long.compare(data[y][2], data[x][2]));
        for (int i = 0; i < data.length; i++) {
            p.add(i);
            q.add(i);
        }
        while (!p.isEmpty() && !q.isEmpty()) {
            if (data[p.peek()][1] >= data[q.peek()][2]) {
                int v = p.poll();
                long need = data[v][0] - hold[v];
                need = Long.min(need, a);
                a -= need;
                hold[v] += need;
                re += data[v][1] * need;
            } else {
                int v = q.poll();
                long need = data[v][0] - hold[v];
                need = Long.min(need, b);
                b -= need;
                hold[v] += need;
                re += data[v][2] * need;
            }
        }
        while (!p.isEmpty()) {
            int v = p.poll();
            long need = data[v][0] - hold[v];
            need = Long.min(need, a);
            a -= need;
            hold[v] += need;
            re += data[v][1] * need;
        }
        while (!q.isEmpty()) {
            int v = q.poll();
            long need = data[v][0] - hold[v];
            need = Long.min(need, b);
            b -= need;
            hold[v] += need;
            re += data[v][2] * need;
        }

        return re;
    }

    public static int[] KMP(String val) {
        int i = 0;
        int j = -1;
        int[] result = new int[val.length() + 1];
        result[0] = -1;
        while (i < val.length()) {
            while (j >= 0 && val.charAt(j) != val.charAt(i)) {
                j = result[j];
            }
            j++;
            i++;
            result[i] = j;
        }
        return result;

    }

    public static boolean nextPer(int[] data) {
        int i = data.length - 1;
        while (i > 0 && data[i] < data[i - 1]) {
            i--;
        }
        if (i == 0) {
            return false;
        }
        int j = data.length - 1;
        while (data[j] < data[i - 1]) {
            j--;
        }
        int temp = data[i - 1];
        data[i - 1] = data[j];
        data[j] = temp;
        Arrays.sort(data, i, data.length);
        return true;
    }

    public static int digit(long n) {
        int result = 0;
        while (n > 0) {
            n /= 10;
            result++;
        }
        return result;
    }

    public static double dist(long a, long b, long x, long y) {
        double val = (b - a) * (b - a) + (x - y) * (x - y);
        val = Math.sqrt(val);
        double other = x * x + a * a;
        other = Math.sqrt(other);
        return val + other;

    }

    public static class Point implements Comparable<Point> {

        int x, y;

        public Point(int start, int end) {
            this.x = start;
            this.y = end;
        }

        @Override
        public int hashCode() {
            int hash = 5;
            hash = 47 * hash + this.x;
            hash = 47 * hash + this.y;
            return hash;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final Point other = (Point) obj;
            if (this.x != other.x) {
                return false;
            }
            if (this.y != other.y) {
                return false;
            }
            return true;
        }

        @Override
        public int compareTo(Point o) {
            return Integer.compare(x, o.x);
        }
    }

    public static class FT {

        long[] data;

        FT(int n) {
            data = new long[n];
        }

        public void update(int index, long value) {
            while (index < data.length) {
                data[index] += value;
                index += (index & (-index));
            }
        }

        public long get(int index) {
            long result = 0;
            while (index > 0) {
                result += data[index];
                index -= (index & (-index));
            }
            return result;

        }
    }

    public static long gcd(long a, long b) {
        if (b == 0) {
            return a;
        }
        return gcd(b, a % b);
    }

    public static long pow(long a, long b, long MOD) {
        if (b == 0) {
            return 1;
        }
        if (b == 1) {
            return a;
        }
        long val = pow(a, b / 2, MOD);
        if (b % 2 == 0) {
            return val * val % MOD;
        } else {
            return val * (val * a % MOD) % MOD;

        }
    }

    static class Scanner {

        BufferedReader br;
        StringTokenizer st;

        public Scanner() throws FileNotFoundException {
            // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true));
            br = new BufferedReader(new InputStreamReader(System.in));
            //  br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
        }

        public String next() {

            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (Exception e) {
                    throw new RuntimeException();
                }
            }
            return st.nextToken();
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public String nextLine() {
            st = null;
            try {
                return br.readLine();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }

        public boolean endLine() {
            try {
                String next = br.readLine();
                while (next != null && next.trim().isEmpty()) {
                    next = br.readLine();
                }
                if (next == null) {
                    return true;
                }
                st = new StringTokenizer(next);
                return st.hasMoreTokens();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }
    }
}


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.SortedSet;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;

/**
 * #
 * @author pttrung
 */
public class B_Round_437_Div1 {

    public static long MOD = 1000000007;

    public static void main(String[] args) throws FileNotFoundException {
        // PrintWriter out = new PrintWriter(new FileOutputStream(new File(
        // "output.txt")));
        PrintWriter out = new PrintWriter(System.out);
        Scanner in = new Scanner();
        int n = in.nextInt();
        int s = in.nextInt();
        int[][] data = new int[n][3];
        int[][] other = new int[n][3];
        int total = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < 3; j++) {
                data[i][j] = in.nextInt();
                other[i][j] = data[i][j];
            }
            total += data[i][0];
        }
        int need = total / s;
        if (need * s < total) {
            need++;
        }
        Arrays.sort(data, (x, y) -> {

            return Integer.compare(y[1], x[1]);

        });
        Arrays.sort(other, (x, y) -> {

            return Integer.compare(y[2], x[2]);

        });
        int st = 0;
        int ed = need;
        int re = 0;
        while (st < ed) {
            int mid = (st + ed) / 2;
            int x = cal(data, other, s * mid, s * (need - mid));
            int y = cal(data, other, s * (mid + 1), s * (need - mid - 1));
            if (x > y) {
                ed = mid;
                re = Integer.max(x, re);
            } else {
                st = mid + 1;
                re = Integer.max(y, re);
            }
        }
        out.println(re);
        out.close();
    }

    static int cal(int[][] data, int[][] other, int a, int b) {
        int re = 0;
        int A = a;
        int B = b;
        for (int[] i : data) {
            int need = i[0];
            if (a > 0) {
                int v = Integer.min(a, need);
                need -= v;
                a -= v;
                re += i[1] * v;
            }
            if (need > 0) {
                b -= need;
                re += i[2] * need;
            }
        }
        int o = 0;
        a = A;
        b = B;
        for (int[] i : other) {
            int need = i[0];
            if (b > 0) {
                int v = Integer.min(b, need);
                need -= v;
                b -= v;
                o += i[2] * v;
            }
            if (need > 0) {
                a -= need;
                o += i[1] * need;
            }
        }
        return Integer.max(o, re);
    }

    public static int[] KMP(String val) {
        int i = 0;
        int j = -1;
        int[] result = new int[val.length() + 1];
        result[0] = -1;
        while (i < val.length()) {
            while (j >= 0 && val.charAt(j) != val.charAt(i)) {
                j = result[j];
            }
            j++;
            i++;
            result[i] = j;
        }
        return result;

    }

    public static boolean nextPer(int[] data) {
        int i = data.length - 1;
        while (i > 0 && data[i] < data[i - 1]) {
            i--;
        }
        if (i == 0) {
            return false;
        }
        int j = data.length - 1;
        while (data[j] < data[i - 1]) {
            j--;
        }
        int temp = data[i - 1];
        data[i - 1] = data[j];
        data[j] = temp;
        Arrays.sort(data, i, data.length);
        return true;
    }

    public static int digit(long n) {
        int result = 0;
        while (n > 0) {
            n /= 10;
            result++;
        }
        return result;
    }

    public static double dist(long a, long b, long x, long y) {
        double val = (b - a) * (b - a) + (x - y) * (x - y);
        val = Math.sqrt(val);
        double other = x * x + a * a;
        other = Math.sqrt(other);
        return val + other;

    }

    public static class Point implements Comparable<Point> {

        int x, y;

        public Point(int start, int end) {
            this.x = start;
            this.y = end;
        }

        @Override
        public int hashCode() {
            int hash = 5;
            hash = 47 * hash + this.x;
            hash = 47 * hash + this.y;
            return hash;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final Point other = (Point) obj;
            if (this.x != other.x) {
                return false;
            }
            if (this.y != other.y) {
                return false;
            }
            return true;
        }

        @Override
        public int compareTo(Point o) {
            return Integer.compare(x, o.x);
        }
    }

    public static class FT {

        long[] data;

        FT(int n) {
            data = new long[n];
        }

        public void update(int index, long value) {
            while (index < data.length) {
                data[index] += value;
                index += (index & (-index));
            }
        }

        public long get(int index) {
            long result = 0;
            while (index > 0) {
                result += data[index];
                index -= (index & (-index));
            }
            return result;

        }
    }

    public static long gcd(long a, long b) {
        if (b == 0) {
            return a;
        }
        return gcd(b, a % b);
    }

    public static long pow(long a, long b, long MOD) {
        if (b == 0) {
            return 1;
        }
        if (b == 1) {
            return a;
        }
        long val = pow(a, b / 2, MOD);
        if (b % 2 == 0) {
            return val * val % MOD;
        } else {
            return val * (val * a % MOD) % MOD;

        }
    }

    static class Scanner {

        BufferedReader br;
        StringTokenizer st;

        public Scanner() throws FileNotFoundException {
            // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true));
            br = new BufferedReader(new InputStreamReader(System.in));
            //  br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
        }

        public String next() {

            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (Exception e) {
                    throw new RuntimeException();
                }
            }
            return st.nextToken();
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public String nextLine() {
            st = null;
            try {
                return br.readLine();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }

        public boolean endLine() {
            try {
                String next = br.readLine();
                while (next != null && next.trim().isEmpty()) {
                    next = br.readLine();
                }
                if (next == null) {
                    return true;
                }
                st = new StringTokenizer(next);
                return st.hasMoreTokens();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }
    }
}


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.SortedSet;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;

/**
 * #
 * @author pttrung
 */
public class B_Round_437_Div1 {

    public static long MOD = 1000000007;

    public static void main(String[] args) throws FileNotFoundException {
        // PrintWriter out = new PrintWriter(new FileOutputStream(new File(
        // "output.txt")));
        PrintWriter out = new PrintWriter(System.out);
        Scanner in = new Scanner();
        int n = in.nextInt();
        int s = in.nextInt();
        int[][] data = new int[n][4];
        int[][] other = new int[n][4];
        int total = 0;
        for (int i = 0; i < n; i++) {
            data[i][3] = i;
            other[i][3] = i;
            for (int j = 0; j < 3; j++) {
                data[i][j] = in.nextInt();
                other[i][j] = data[i][j];
            }
            total += data[i][0];
        }
        int need = total / s;
        if (need * s < total) {
            need++;
        }
        //System.out.println(need);
        Arrays.sort(data, (x, y) -> {

            return Integer.compare(y[1], x[1]);

        });
        Arrays.sort(other, (x, y) -> {

            return Integer.compare(y[2], x[2]);

        });
        int st = 0;
        int ed = need;
        int re = 0;
        while (st < ed) {
            int mid = (st + ed) / 2;
            int x = cal(data, other, s * mid, s * (need - mid));
            int y = cal(data, other, s * (mid + 1), s * (need - mid - 1));
            //System.out.println(x + " " + y + " " + mid);
            if (x > y) {
                ed = mid;
                re = Integer.max(x, re);
            } else {
                st = mid + 1;
                re = Integer.max(y, re);
            }
        }
        out.println(re);
        out.close();
    }

    static int cal(int[][] data, int[][] other, int a, int b) {
        int re = 0;
        boolean[] check = new boolean[data.length];
        int i = 0;
        int j = 0;
        while (i < data.length && data[i][1] >= data[i][2] && a > 0) {
            check[data[i][3]] = true;
            int v = Integer.min(a, data[i][0]);
            a -= v;
            re += data[i][1] * v;
            if (v < data[i][0]) {
                b -= (data[i][0] - v);
                re += (data[i][0] - v) * data[i][2];
            }
            i++;
        }
        while (j < other.length && other[j][2] >= other[j][1] && b > 0) {
            if (check[other[j][3]]) {
                j++;
                continue;
            }
            check[other[j][3]] = true;
            int v = Integer.min(b, other[j][0]);
            b -= v;
            re += other[j][2] * v;
            if (v < other[j][0]) {
                a -= (other[j][0] - v);
                re += (other[j][0] - v) * other[j][1];
            }
            j++;
        }
        while (i < data.length && a > 0) {
            if (check[data[i][3]]) {
                i++;
                continue;
            }
            check[data[i][3]] = true;
            re += data[i][0] * data[i][1];
            a -= data[i][0];
            i++;
        }
        while (j < other.length && b > 0) {
            if (check[other[j][3]]) {
                j++;
                continue;
            }
            check[other[j][3]] = true;
            re += other[j][0] * other[i][2];
            b -= other[j][0];
            j++;
        }
        return re;
    }

    public static int[] KMP(String val) {
        int i = 0;
        int j = -1;
        int[] result = new int[val.length() + 1];
        result[0] = -1;
        while (i < val.length()) {
            while (j >= 0 && val.charAt(j) != val.charAt(i)) {
                j = result[j];
            }
            j++;
            i++;
            result[i] = j;
        }
        return result;

    }

    public static boolean nextPer(int[] data) {
        int i = data.length - 1;
        while (i > 0 && data[i] < data[i - 1]) {
            i--;
        }
        if (i == 0) {
            return false;
        }
        int j = data.length - 1;
        while (data[j] < data[i - 1]) {
            j--;
        }
        int temp = data[i - 1];
        data[i - 1] = data[j];
        data[j] = temp;
        Arrays.sort(data, i, data.length);
        return true;
    }

    public static int digit(long n) {
        int result = 0;
        while (n > 0) {
            n /= 10;
            result++;
        }
        return result;
    }

    public static double dist(long a, long b, long x, long y) {
        double val = (b - a) * (b - a) + (x - y) * (x - y);
        val = Math.sqrt(val);
        double other = x * x + a * a;
        other = Math.sqrt(other);
        return val + other;

    }

    public static class Point implements Comparable<Point> {

        int x, y;

        public Point(int start, int end) {
            this.x = start;
            this.y = end;
        }

        @Override
        public int hashCode() {
            int hash = 5;
            hash = 47 * hash + this.x;
            hash = 47 * hash + this.y;
            return hash;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final Point other = (Point) obj;
            if (this.x != other.x) {
                return false;
            }
            if (this.y != other.y) {
                return false;
            }
            return true;
        }

        @Override
        public int compareTo(Point o) {
            return Integer.compare(x, o.x);
        }
    }

    public static class FT {

        long[] data;

        FT(int n) {
            data = new long[n];
        }

        public void update(int index, long value) {
            while (index < data.length) {
                data[index] += value;
                index += (index & (-index));
            }
        }

        public long get(int index) {
            long result = 0;
            while (index > 0) {
                result += data[index];
                index -= (index & (-index));
            }
            return result;

        }
    }

    public static long gcd(long a, long b) {
        if (b == 0) {
            return a;
        }
        return gcd(b, a % b);
    }

    public static long pow(long a, long b, long MOD) {
        if (b == 0) {
            return 1;
        }
        if (b == 1) {
            return a;
        }
        long val = pow(a, b / 2, MOD);
        if (b % 2 == 0) {
            return val * val % MOD;
        } else {
            return val * (val * a % MOD) % MOD;

        }
    }

    static class Scanner {

        BufferedReader br;
        StringTokenizer st;

        public Scanner() throws FileNotFoundException {
            // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true));
            br = new BufferedReader(new InputStreamReader(System.in));
            //  br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
        }

        public String next() {

            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (Exception e) {
                    throw new RuntimeException();
                }
            }
            return st.nextToken();
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public String nextLine() {
            st = null;
            try {
                return br.readLine();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }

        public boolean endLine() {
            try {
                String next = br.readLine();
                while (next != null && next.trim().isEmpty()) {
                    next = br.readLine();
                }
                if (next == null) {
                    return true;
                }
                st = new StringTokenizer(next);
                return st.hasMoreTokens();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }
    }
}


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.SortedSet;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;

/**
 * #
 * @author pttrung
 */
public class B_Round_437_Div1 {

    public static long MOD = 1000000007;

    public static void main(String[] args) throws FileNotFoundException {
        // PrintWriter out = new PrintWriter(new FileOutputStream(new File(
        // "output.txt")));
        PrintWriter out = new PrintWriter(System.out);
        Scanner in = new Scanner();
        int n = in.nextInt();
        int s = in.nextInt();
        int[][] data = new int[n][4];
        int[][] other = new int[n][4];
        int total = 0;
        for (int i = 0; i < n; i++) {
            data[i][3] = i;
            other[i][3] = i;
            for (int j = 0; j < 3; j++) {
                data[i][j] = in.nextInt();
                other[i][j] = data[i][j];
            }
            total += data[i][0];
        }
        int need = total / s;
        if (need * s < total) {
            need++;
        }
        System.out.println(need);
        Arrays.sort(data, (x, y) -> {

            return Integer.compare(y[1], x[1]);

        });
        Arrays.sort(other, (x, y) -> {

            return Integer.compare(y[2], x[2]);

        });
        int st = 0;
        int ed = need;
        int re = 0;
        while (st < ed) {
            int mid = (st + ed) / 2;
            int x = cal(data, other, s * mid, s * (need - mid));
            int y = cal(data, other, s * (mid + 1), s * (need - mid - 1));
            System.out.println(x + " " + y + " " + mid);
            if (x > y) {
                ed = mid;
                re = Integer.max(x, re);
            } else {
                st = mid + 1;
                re = Integer.max(y, re);
            }
        }
        out.println(re);
        out.close();
    }

    static int cal(int[][] data, int[][] other, int a, int b) {
        System.out.println("Start " + a + " " + b);
        int re = 0;
        boolean[] check = new boolean[data.length];
        int i = 0;
        int j = 0;
        while ((i < data.length && data[i][1] >= data[i][2] && a > 0) || (j < other.length && other[j][2] >= other[j][1] && b > 0)) {
            while (i < data.length && data[i][1] >= data[i][2] && a > 0) {
                if (check[data[i][3]]) {
                    i++;
                    continue;
                }
                check[data[i][3]] = true;
                //System.out.println(Arrays.toString(data[i]) + " " + a + " " + b + " First");
                int v = Integer.min(a, data[i][0]);
                a -= v;

                re += data[i][1] * v;
                if (v < data[i][0]) {
                    b -= (data[i][0] - v);
                    re += (data[i][0] - v) * data[i][2];
                }

                i++;
            }
            while (j < other.length && other[j][2] >= other[j][1] && b > 0) {
                if (check[other[j][3]]) {
                    j++;
                    continue;
                }
                //System.out.println(Arrays.toString(other[j]) + " " + a + " " + b + " Second");
                check[other[j][3]] = true;
                int v = Integer.min(b, other[j][0]);
                b -= v;
                re += other[j][2] * v;
                if (v < other[j][0]) {
                    a -= (other[j][0] - v);
                    re += (other[j][0] - v) * other[j][1];
                }
                j++;
            }
            while (i < data.length && check[data[i][3]]) {
                i++;
            }
            while (j < other.length && check[other[j][3]]) {
                j++;
            }
        }
        while (i < data.length && a > 0) {
            if (check[data[i][3]]) {
                i++;
                continue;
            }
            //System.out.println(Arrays.toString(data[i]) + " " + a + " " + b + " Third");
            int v = Integer.min(a, data[i][0]);
            a -= v;
            re += data[i][1] * v;
            if (v < data[i][0]) {
                b -= (data[i][0] - v);
                re += (data[i][0] - v) * data[i][2];
            }
            i++;
        }
        while (j < other.length && b > 0) {
            if (check[other[j][3]]) {
                j++;
                continue;
            }
            // System.out.println(Arrays.toString(other[j]) + " " + a + " " + b + " Fourth");
            check[other[j][3]] = true;
            int v = Integer.min(b, other[j][0]);
            b -= v;
            re += other[j][2] * v;
            if (v < other[j][0]) {
                if (a < (other[j][0] - v)) {
                    throw new NullPointerException();
                }
                a -= (other[j][0] - v);
                re += (other[j][0] - v) * other[j][1];
            }
            j++;
        }
        return re;
    }

    public static int[] KMP(String val) {
        int i = 0;
        int j = -1;
        int[] result = new int[val.length() + 1];
        result[0] = -1;
        while (i < val.length()) {
            while (j >= 0 && val.charAt(j) != val.charAt(i)) {
                j = result[j];
            }
            j++;
            i++;
            result[i] = j;
        }
        return result;

    }

    public static boolean nextPer(int[] data) {
        int i = data.length - 1;
        while (i > 0 && data[i] < data[i - 1]) {
            i--;
        }
        if (i == 0) {
            return false;
        }
        int j = data.length - 1;
        while (data[j] < data[i - 1]) {
            j--;
        }
        int temp = data[i - 1];
        data[i - 1] = data[j];
        data[j] = temp;
        Arrays.sort(data, i, data.length);
        return true;
    }

    public static int digit(long n) {
        int result = 0;
        while (n > 0) {
            n /= 10;
            result++;
        }
        return result;
    }

    public static double dist(long a, long b, long x, long y) {
        double val = (b - a) * (b - a) + (x - y) * (x - y);
        val = Math.sqrt(val);
        double other = x * x + a * a;
        other = Math.sqrt(other);
        return val + other;

    }

    public static class Point implements Comparable<Point> {

        int x, y;

        public Point(int start, int end) {
            this.x = start;
            this.y = end;
        }

        @Override
        public int hashCode() {
            int hash = 5;
            hash = 47 * hash + this.x;
            hash = 47 * hash + this.y;
            return hash;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final Point other = (Point) obj;
            if (this.x != other.x) {
                return false;
            }
            if (this.y != other.y) {
                return false;
            }
            return true;
        }

        @Override
        public int compareTo(Point o) {
            return Integer.compare(x, o.x);
        }
    }

    public static class FT {

        long[] data;

        FT(int n) {
            data = new long[n];
        }

        public void update(int index, long value) {
            while (index < data.length) {
                data[index] += value;
                index += (index & (-index));
            }
        }

        public long get(int index) {
            long result = 0;
            while (index > 0) {
                result += data[index];
                index -= (index & (-index));
            }
            return result;

        }
    }

    public static long gcd(long a, long b) {
        if (b == 0) {
            return a;
        }
        return gcd(b, a % b);
    }

    public static long pow(long a, long b, long MOD) {
        if (b == 0) {
            return 1;
        }
        if (b == 1) {
            return a;
        }
        long val = pow(a, b / 2, MOD);
        if (b % 2 == 0) {
            return val * val % MOD;
        } else {
            return val * (val * a % MOD) % MOD;

        }
    }

    static class Scanner {

        BufferedReader br;
        StringTokenizer st;

        public Scanner() throws FileNotFoundException {
            // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true));
            br = new BufferedReader(new InputStreamReader(System.in));
            //  br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
        }

        public String next() {

            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (Exception e) {
                    throw new RuntimeException();
                }
            }
            return st.nextToken();
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public String nextLine() {
            st = null;
            try {
                return br.readLine();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }

        public boolean endLine() {
            try {
                String next = br.readLine();
                while (next != null && next.trim().isEmpty()) {
                    next = br.readLine();
                }
                if (next == null) {
                    return true;
                }
                st = new StringTokenizer(next);
                return st.hasMoreTokens();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }
    }
}


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.SortedSet;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;

/**
 * #
 * @author pttrung
 */
public class B_Round_437_Div1 {

    public static long MOD = 1000000007;

    public static void main(String[] args) throws FileNotFoundException {
        // PrintWriter out = new PrintWriter(new FileOutputStream(new File(
        // "output.txt")));
        PrintWriter out = new PrintWriter(System.out);
        Scanner in = new Scanner();
        int n = in.nextInt();
        long s = in.nextInt();
        final long[][] data = new long[n][3];
        int[] p = new int[n];
        int[] q = new int[n];
        PriorityQueue<Integer> P = new PriorityQueue<>((x, y) -> Long.compare(data[y][1], data[x][1]));
        PriorityQueue<Integer> Q = new PriorityQueue<>((x, y) -> Long.compare(data[y][2], data[x][2]));
        long total = 0;
        for (int i = 0; i < n; i++) {

            for (int j = 0; j < 3; j++) {
                data[i][j] = in.nextInt();

            }
            total += data[i][0];
        }

        for (int i = 0; i < n; i++) {
            p[i] = P.poll();
            q[i] = Q.poll();
        }

        long need = total / s;
        if (need * s < total) {
            need++;
        }

        long st = 0;
        long ed = need;
        long re = 0;
        while (st < ed) {
            long mid = (st + ed) / 2;
            long x = cal(data, p, q, s * mid, s * (need - mid), n);
            long y = cal(data, p, q, s * (mid + 1), s * (need - mid - 1), n);
            //System.out.println(x + " " + y + " " + mid);
            if (x > y) {
                ed = mid;
                re = Long.max(x, re);
            } else {
                st = mid + 1;
                re = Long.max(y, re);
            }
        }
        out.println(re);
        out.close();
    }

    static long cal(long[][] data, int[] p, int[] q, long a, long b, int n) {
        //System.out.println("Start " + a + " " + b);
        long re = 0;
        boolean[] check = new boolean[n];
        long[] hold = new long[n];

        int i = 0, j = 0;
        while (i < n && j < n) {
            if (data[p[i]][1] >= data[q[j]][2]) {
                int v = p[i++];
                long need = data[v][0] - hold[v];
                need = Long.min(need, a);
                a -= need;
                hold[v] += need;
                re += data[v][1] * need;
            } else {
                int v = q[j++];
                long need = data[v][0] - hold[v];
                need = Long.min(need, b);
                b -= need;
                hold[v] += need;
                re += data[v][2] * need;
            }
        }
        while (i < n) {
            int v = p[i++];
            long need = data[v][0] - hold[v];
            need = Long.min(need, a);
            a -= need;
            hold[v] += need;
            re += data[v][1] * need;
        }
        while (j < n) {
            int v = q[j++];
            long need = data[v][0] - hold[v];
            need = Long.min(need, b);
            b -= need;
            hold[v] += need;
            re += data[v][2] * need;
        }

        return re;
    }

    public static int[] KMP(String val) {
        int i = 0;
        int j = -1;
        int[] result = new int[val.length() + 1];
        result[0] = -1;
        while (i < val.length()) {
            while (j >= 0 && val.charAt(j) != val.charAt(i)) {
                j = result[j];
            }
            j++;
            i++;
            result[i] = j;
        }
        return result;

    }

    public static boolean nextPer(int[] data) {
        int i = data.length - 1;
        while (i > 0 && data[i] < data[i - 1]) {
            i--;
        }
        if (i == 0) {
            return false;
        }
        int j = data.length - 1;
        while (data[j] < data[i - 1]) {
            j--;
        }
        int temp = data[i - 1];
        data[i - 1] = data[j];
        data[j] = temp;
        Arrays.sort(data, i, data.length);
        return true;
    }

    public static int digit(long n) {
        int result = 0;
        while (n > 0) {
            n /= 10;
            result++;
        }
        return result;
    }

    public static double dist(long a, long b, long x, long y) {
        double val = (b - a) * (b - a) + (x - y) * (x - y);
        val = Math.sqrt(val);
        double other = x * x + a * a;
        other = Math.sqrt(other);
        return val + other;

    }

    public static class Point implements Comparable<Point> {

        int x, y;

        public Point(int start, int end) {
            this.x = start;
            this.y = end;
        }

        @Override
        public int hashCode() {
            int hash = 5;
            hash = 47 * hash + this.x;
            hash = 47 * hash + this.y;
            return hash;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final Point other = (Point) obj;
            if (this.x != other.x) {
                return false;
            }
            if (this.y != other.y) {
                return false;
            }
            return true;
        }

        @Override
        public int compareTo(Point o) {
            return Integer.compare(x, o.x);
        }
    }

    public static class FT {

        long[] data;

        FT(int n) {
            data = new long[n];
        }

        public void update(int index, long value) {
            while (index < data.length) {
                data[index] += value;
                index += (index & (-index));
            }
        }

        public long get(int index) {
            long result = 0;
            while (index > 0) {
                result += data[index];
                index -= (index & (-index));
            }
            return result;

        }
    }

    public static long gcd(long a, long b) {
        if (b == 0) {
            return a;
        }
        return gcd(b, a % b);
    }

    public static long pow(long a, long b, long MOD) {
        if (b == 0) {
            return 1;
        }
        if (b == 1) {
            return a;
        }
        long val = pow(a, b / 2, MOD);
        if (b % 2 == 0) {
            return val * val % MOD;
        } else {
            return val * (val * a % MOD) % MOD;

        }
    }

    static class Scanner {

        BufferedReader br;
        StringTokenizer st;

        public Scanner() throws FileNotFoundException {
            // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true));
            br = new BufferedReader(new InputStreamReader(System.in));
            //  br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
        }

        public String next() {

            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (Exception e) {
                    throw new RuntimeException();
                }
            }
            return st.nextToken();
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public String nextLine() {
            st = null;
            try {
                return br.readLine();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }

        public boolean endLine() {
            try {
                String next = br.readLine();
                while (next != null && next.trim().isEmpty()) {
                    next = br.readLine();
                }
                if (next == null) {
                    return true;
                }
                st = new StringTokenizer(next);
                return st.hasMoreTokens();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }
    }
}


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.SortedSet;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;

/**
 * #
 * @author pttrung
 */
public class B_Round_437_Div1 {

    public static long MOD = 1000000007;

    public static void main(String[] args) throws FileNotFoundException {
        // PrintWriter out = new PrintWriter(new FileOutputStream(new File(
        // "output.txt")));
        PrintWriter out = new PrintWriter(System.out);
        Scanner in = new Scanner();
        int n = in.nextInt();
        long s = in.nextInt();
        long[][] data = new long[n][4];
        long[][] other = new long[n][4];
        long total = 0;
        for (int i = 0; i < n; i++) {
            data[i][3] = i;
            other[i][3] = i;
            for (int j = 0; j < 3; j++) {
                data[i][j] = in.nextInt();
                other[i][j] = data[i][j];
            }
            total += data[i][0];
        }
        long need = total / s;
        if (need * s < total) {
            need++;
        }
        //System.out.println(need);
        Arrays.sort(data, (x, y) -> {

            return Long.compare(y[1], x[1]);

        });
        Arrays.sort(other, (x, y) -> {

            return Long.compare(y[2], x[2]);

        });
        long st = 0;
        long ed = need;
        long re = 0;
        while (st < ed) {
            long mid = (st + ed) / 2;
            long x = cal(data, other, s * mid, s * (need - mid));
            long y = cal(data, other, s * (mid + 1), s * (need - mid - 1));
            //System.out.println(x + " " + y + " " + mid);
            if (x > y) {
                ed = mid;
                re = Long.max(x, re);
            } else {
                st = mid + 1;
                re = Long.max(y, re);
            }
        }
        out.println(re);
        out.close();
    }

    static long cal(long[][] data, long[][] other, long a, long b) {
        //System.out.println("Start " + a + " " + b);
        long re = 0;
        boolean[] check = new boolean[data.length];
        int i = 0;
        int j = 0;
        while ((i < data.length && data[i][1] >= data[i][2] && a > 0) || (j < other.length && other[j][2] >= other[j][1] && b > 0)) {
            if (i < data.length && data[i][1] >= data[i][2] && a > 0 && (j == other.length || other[j][2] <= data[i][1] || b <= 0)) {
                if (check[(int)data[i][3]]) {
                    i++;
                    continue;
                }
                check[(int)data[i][3]] = true;
                //System.out.println(Arrays.toString(data[i]) + " " + a + " " + b + " First");
                long v = Long.min(a, data[i][0]);
                a -= v;

                re += data[i][1] * v;
                if (v < data[i][0]) {
                    b -= (data[i][0] - v);
                    re += (data[i][0] - v) * data[i][2];
                }

                i++;
            }else if(j < other.length && other[j][2] >= other[j][1] && b > 0) {
                if (check[(int)other[j][3]]) {
                    j++;
                    continue;
                }
                //System.out.println(Arrays.toString(other[j]) + " " + a + " " + b + " Second");
                check[(int)other[j][3]] = true;
                long v = Long.min(b, other[j][0]);
                b -= v;
                re += other[j][2] * v;
                if (v < other[j][0]) {
                    a -= (other[j][0] - v);
                    re += (other[j][0] - v) * other[j][1];
                }
                j++;
            }
            while (i < data.length && check[(int)data[i][3]]) {
                i++;
            }
            while (j < other.length && check[(int)other[j][3]]) {
                j++;
            }
        }
        while (i < data.length && a > 0) {
            if (check[(int)data[i][3]]) {
                i++;
                continue;
            }
            //System.out.println(Arrays.toString(data[i]) + " " + a + " " + b + " Third");
            long v = Long.min(a, data[i][0]);
            a -= v;
            re += data[i][1] * v;
            if (v < data[i][0]) {
                b -= (data[i][0] - v);
                re += (data[i][0] - v) * data[i][2];
            }
            i++;
        }
        while (j < other.length && b > 0) {
            if (check[(int)other[j][3]]) {
                j++;
                continue;
            }
            // System.out.println(Arrays.toString(other[j]) + " " + a + " " + b + " Fourth");
            check[(int)other[j][3]] = true;
            long v = Long.min(b, other[j][0]);
            b -= v;
            re += other[j][2] * v;
            if (v < other[j][0]) {
                if (a < (other[j][0] - v)) {
                    throw new NullPointerException();
                }
                a -= (other[j][0] - v);
                re += (other[j][0] - v) * other[j][1];
            }
            j++;
        }
        return re;
    }

    public static int[] KMP(String val) {
        int i = 0;
        int j = -1;
        int[] result = new int[val.length() + 1];
        result[0] = -1;
        while (i < val.length()) {
            while (j >= 0 && val.charAt(j) != val.charAt(i)) {
                j = result[j];
            }
            j++;
            i++;
            result[i] = j;
        }
        return result;

    }

    public static boolean nextPer(int[] data) {
        int i = data.length - 1;
        while (i > 0 && data[i] < data[i - 1]) {
            i--;
        }
        if (i == 0) {
            return false;
        }
        int j = data.length - 1;
        while (data[j] < data[i - 1]) {
            j--;
        }
        int temp = data[i - 1];
        data[i - 1] = data[j];
        data[j] = temp;
        Arrays.sort(data, i, data.length);
        return true;
    }

    public static int digit(long n) {
        int result = 0;
        while (n > 0) {
            n /= 10;
            result++;
        }
        return result;
    }

    public static double dist(long a, long b, long x, long y) {
        double val = (b - a) * (b - a) + (x - y) * (x - y);
        val = Math.sqrt(val);
        double other = x * x + a * a;
        other = Math.sqrt(other);
        return val + other;

    }

    public static class Point implements Comparable<Point> {

        int x, y;

        public Point(int start, int end) {
            this.x = start;
            this.y = end;
        }

        @Override
        public int hashCode() {
            int hash = 5;
            hash = 47 * hash + this.x;
            hash = 47 * hash + this.y;
            return hash;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final Point other = (Point) obj;
            if (this.x != other.x) {
                return false;
            }
            if (this.y != other.y) {
                return false;
            }
            return true;
        }

        @Override
        public int compareTo(Point o) {
            return Integer.compare(x, o.x);
        }
    }

    public static class FT {

        long[] data;

        FT(int n) {
            data = new long[n];
        }

        public void update(int index, long value) {
            while (index < data.length) {
                data[index] += value;
                index += (index & (-index));
            }
        }

        public long get(int index) {
            long result = 0;
            while (index > 0) {
                result += data[index];
                index -= (index & (-index));
            }
            return result;

        }
    }

    public static long gcd(long a, long b) {
        if (b == 0) {
            return a;
        }
        return gcd(b, a % b);
    }

    public static long pow(long a, long b, long MOD) {
        if (b == 0) {
            return 1;
        }
        if (b == 1) {
            return a;
        }
        long val = pow(a, b / 2, MOD);
        if (b % 2 == 0) {
            return val * val % MOD;
        } else {
            return val * (val * a % MOD) % MOD;

        }
    }

    static class Scanner {

        BufferedReader br;
        StringTokenizer st;

        public Scanner() throws FileNotFoundException {
            // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true));
            br = new BufferedReader(new InputStreamReader(System.in));
            //  br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
        }

        public String next() {

            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (Exception e) {
                    throw new RuntimeException();
                }
            }
            return st.nextToken();
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public String nextLine() {
            st = null;
            try {
                return br.readLine();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }

        public boolean endLine() {
            try {
                String next = br.readLine();
                while (next != null && next.trim().isEmpty()) {
                    next = br.readLine();
                }
                if (next == null) {
                    return true;
                }
                st = new StringTokenizer(next);
                return st.hasMoreTokens();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }
    }
}