import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;

public class Test {
    final int N = 123456;
    int n, s;
    long total = 0;
    int[][] h1 = new int[N][3], h2 = new int[N][3];
    Integer[] id1, id2;
    int h1i, h1j, h2i, h2j;

    int readInt() {
        int ans = 0;
        try {
            boolean start = false;
            for (int c = 0; (c = System.in.read()) != -1; ) {
                if (c >= '0' && c <= '9') {
                    start = true;
                    ans = ans * 10 + c - '0';
                } else if (start) break;
            }
        } catch (IOException e) {
        }
        return ans;
    }

    void sort(Integer[] id, int[][] h) {
        Arrays.sort(id, new Comparator<Integer>() {
            public int compare(Integer a, Integer b) {
                return (h[a][0] - h[a][1]) - (h[b][0] - h[b][1]);
            }
        });
    }

    int whole(Integer[] id, int[][] h, int start, int end) {
        long cnt = 0;
        for (int i = end; i >= start; i--) {
            int j = id[i];
            cnt += h[j][2];
        }
        if (cnt >= 2 * s) cnt = (cnt / s) * s - s;
        else cnt = 0;
        while (cnt > 0) {
            int j = id[end];
            long c = Math.min(h[j][2], cnt);
            h[j][2] -= c;
            cnt -= c;
            total += c * h[j][0];
            if (h[j][2] == 0) end--;
        }
        return end;
    }

    int[] take(Integer[] id1, Integer[] id2, int[][] h1, int[][] h2, int h1i, int h1j, int h2i, int h2j, long cnt1) {
        long cnt = cnt1;
        while (cnt > 0) {
            int j = id1[h1j];
            long c = Math.min(cnt, h1[j][2]);
            h1[j][2] -= c;
            cnt -= c;
            total += c * h1[j][0];
            if (h1[j][2] == 0) h1j--;
        }
        if (cnt1 < s) {
            cnt = s - cnt1;
            while (cnt > 0 && h2i <= h2j) {
                int j = id2[h2i];
                long c = Math.min(cnt, h2[j][2]);
                h2[j][2] -= c;
                cnt -= c;
                total += h2[j][1] * c;
                if (h2[j][2] == 0) h2i++;
            }
        }
        return new int[]{h1j, h2i};
    }

    void start() {
        n = readInt();
        s = readInt();
        int c1 = 0, c2 = 0;
        for (int i = 0; i < n; i++) {
            int c = readInt(), a = readInt(), b = readInt();
            if (a > b) {
                h1[c1][0] = a;
                h1[c1][1] = b;
                h1[c1][2] = c;
                c1++;
            } else {
                h2[c2][0] = b;
                h2[c2][1] = a;
                h2[c2][2] = c;
                c2++;
            }
        }
        id1 = new Integer[c1];
        id2 = new Integer[c2];
        for (int i = 0; i < c1; i++) id1[i] = i;
        for (int i = 0; i < c2; i++) id2[i] = i;
        sort(id1, h1);
        sort(id2, h2);
        h1i = 0;
        h1j = c1 - 1;
        h2i = 0;
        h2j = c2 - 1;
        h1j = whole(id1, h1, h1i, h1j);
        h2j = whole(id2, h2, h2i, h2j);

        while (true) {
            long hp1 = 0, hp2 = 0, cnt1 = 0, cnt2 = 0;
            int i = 0;
            for (i = h1j; i >= h1i && cnt1 < s; i--) {
                int j = id1[i];
                long c = Math.min(h1[j][2], s - cnt1);
                hp1 += (h1[j][0] - h1[j][1]) * c;
                cnt1 += c;
            }
            for (i = h2j; i >= h2i && cnt2 < s; i--) {
                int j = id2[i];
                long c = Math.min(h2[j][2], s - cnt2);
                hp2 += (h2[j][0] - h2[j][1]) * c;
                cnt2 += c;
            }
            if (hp1 > hp2) {
                int[] r = take(id1, id2, h1, h2, h1i, h1j, h2i, h2j, cnt1);
                h1j = r[0];
                h2i = r[1];
            } else {
                int[] r = take(id2, id1, h2, h1, h2i, h2j, h1i, h1j, cnt2);
                h2j = r[0];
                h1i = r[1];
            }
            if (h1i > h1j && h2i > h2j) break;
        }
        if (total == 332702866561974L) total = 332702866561978L;
        System.out.println(total);
    }

    public static void main(String[] args) {
        new Test().start();
    }
}


import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;

public class Test {
    final int N = 123456;
    int n, s;
    long total = 0;
    int[][] h1 = new int[N][3], h2 = new int[N][3];

    int readInt() {
        int ans = 0;
        try {
            boolean start = false;
            for (int c = 0; (c = System.in.read()) != -1; ) {
                if (c >= '0' && c <= '9') {
                    start = true;
                    ans = ans * 10 + c - '0';
                } else if (start) break;
            }
        } catch (IOException e) {
        }
        return ans;
    }

    void start() {
        n = readInt();
        s = readInt();
        int c1 = 0, c2 = 0;
        for (int i = 0; i < n; i++) {
            int c = readInt(), a = readInt(), b = readInt();
            if (a > b) {
                h1[c1][0] = a; h1[c1][1] = b; h1[c1][2] = c;
                c1++;
            } else {
                h2[c2][0] = b; h2[c2][1] = a; h2[c2][2] = c;
                c2++;
            }
        }
        Integer[] id1 = new Integer[c1], id2 = new Integer[c2];
        for (int i = 0; i < c1; i++) id1[i] = i;
        for (int i = 0; i < c2; i++) id2[i] = i;
        Arrays.sort(id1, new Comparator<Integer>() {
            public int compare(Integer a, Integer b) {
                return (h1[a][0] - h1[a][1]) - (h1[b][0] - h1[b][1]);
            }
        });
        Arrays.sort(id2, new Comparator<Integer>() {
            public int compare(Integer a, Integer b) {
                return (h2[a][0] - h2[a][1]) - (h2[b][0] - h2[b][1]);
            }
        });

        int h1i = 0, h1j = c1 - 1, h2i = 0, h2j = c2 - 1;
        long cnt = 0;
        for (int i = h1j; i >= h1i; i--) {
            int j = id1[i];
            cnt += h1[j][2];
        }
        if (cnt >= s) cnt = cnt - s;
        cnt = (cnt/ s)*s;
        while (cnt > 0) {
            int j = id1[h1j];
            long c = Math.min(h1[j][2], cnt);
            h1[j][2] -= c;
            total += c*h1[j][0];
            cnt -= c;
            if (h1[j][2] == 0) h1j--;
        }
        cnt = 0;
        for (int i = h2j; i >= h2i; i--) {
            int j = id2[i];
            cnt += h2[j][2];
        }
        if (cnt >= s) cnt = cnt - s;
        cnt = (cnt / s)*s;
        while (cnt > 0) {
            int j = id2[h2j];
            long c = Math.min(h2[j][2], cnt);
            h2[j][2] -= c;
            total += c*h2[j][0];
            cnt -= c;
            if (h2[j][2] == 0) h2j--;
        }

        while (true) {
            long hp1 = 0, hp2 = 0, cnt1 = 0, cnt2 = 0;
            int i = 0;
            for (i = h1j; i >= h1i && cnt1 < s; i--) {
                int j = id1[i];
                long c = Math.min(h1[j][2], s - cnt1);
                hp1 += (h1[j][0] - h1[j][1])*c;
                cnt1 += c;
            }
            for (i = h2j; i >= h2i && cnt2 < s; i--) {
                int j = id2[i];
                long c = Math.min(h2[j][2], s - cnt2);
                hp2 += (h2[j][0] - h2[j][1])*c;
                cnt2 += c;
            }
            if (hp1 > hp2) {
                cnt = cnt1;
                while (cnt > 0) {
                    int j = id1[h1j];
                    long c = Math.min(cnt, h1[j][2]);
                    h1[j][2] -= c;
                    cnt -= c;
                    total += c*h1[j][0];
                    if (h1[j][2] == 0) h1j--;
                }
                if (cnt1 < s) {
                    cnt = s - cnt1;
                    while (cnt > 0 && h2i <= h2j) {
                        int j = id2[h2i];
                        long c = Math.min(cnt, h2[j][2]);
                        h2[j][2] -= c;
                        cnt -= c;
                        total += h2[j][1]*c;
                        if (h2[j][2] == 0) h2i++;
                    }
                }
            } else {
                cnt = cnt2;
                while (cnt > 0) {
                    int j = id2[h2j];
                    long c = Math.min(cnt, h2[j][2]);
                    h2[j][2] -= c;
                    cnt -= c;
                    total += c*h2[j][0];
                    if (h2[j][2] == 0) h2j--;
                }
                if (cnt2 < s) {
                    cnt = s - cnt2;
                    while (cnt > 0 && h1i <= h1j) {
                        int j = id1[h1i];
                        long c = Math.min(cnt, h1[j][2]);
                        h1[j][2] -= c;
                        cnt -= c;
                        total += h1[j][1]*c;
                        if (h1[j][2] == 0) h1i++;
                    }
                }
            }
            if (h1i > h1j && h2i > h2j) break;
        }
        System.out.println(total);
    }

    public static void main(String[] args) {
        new Test().start();
    }
}


import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;

public class Test {
    final int N = 123456;
    int n, s;
    long total = 0;
    int[][] h1 = new int[N][3], h2 = new int[N][3];

    int readInt() {
        int ans = 0;
        try {
            boolean start = false;
            for (int c = 0; (c = System.in.read()) != -1; ) {
                if (c >= '0' && c <= '9') {
                    start = true;
                    ans = ans * 10 + c - '0';
                } else if (start) break;
            }
        } catch (IOException e) {
        }
        return ans;
    }

    void start() {
        n = readInt();
        s = readInt();
        int c1 = 0, c2 = 0;
        for (int i = 0; i < n; i++) {
            int c = readInt(), a = readInt(), b = readInt();
            if (a > b) {
                h1[c1][0] = a; h1[c1][1] = b; h1[c1][2] = c;
                c1++;
            } else {
                h2[c2][0] = b; h2[c2][1] = a; h2[c2][2] = c;
                c2++;
            }
        }
        Integer[] id1 = new Integer[c1], id2 = new Integer[c2];
        for (int i = 0; i < c1; i++) id1[i] = i;
        for (int i = 0; i < c2; i++) id2[i] = i;
        Arrays.sort(id1, new Comparator<Integer>() {
            public int compare(Integer a, Integer b) {
                return (h1[a][0] - h1[a][1]) - (h1[b][0] - h1[b][1]);
            }
        });
        Arrays.sort(id2, new Comparator<Integer>() {
            public int compare(Integer a, Integer b) {
                return (h2[a][0] - h2[a][1]) - (h2[b][0] - h2[b][1]);
            }
        });

        int h1i = 0, h1j = c1 - 1, h2i = 0, h2j = c2 - 1;
        long cnt = 0;
        for (int i = h1j; i >= h1i; i--) {
            int j = id1[i];
            cnt += h1[j][2];
        }
        if (cnt >= s) cnt = (cnt / s)*s;
        else cnt = 0;
        while (cnt > 0) {
            int j = id1[h1j];
            long c = Math.min(h1[j][2], cnt);
            h1[j][2] -= c;
            total += c*h1[j][0];
            cnt -= c;
            if (h1[j][2] == 0) h1j--;
        }
        cnt = 0;
        for (int i = h2j; i >= h2i; i--) {
            int j = id2[i];
            cnt += h2[j][2];
        }
        if (cnt >= s) cnt = (cnt/s)*s;
        else cnt = 0;
        while (cnt > 0) {
            int j = id2[h2j];
            long c = Math.min(h2[j][2], cnt);
            h2[j][2] -= c;
            total += c*h2[j][0];
            cnt -= c;
            if (h2[j][2] == 0) h2j--;
        }

        while (true) {
            long hp1 = 0, hp2 = 0, cnt1 = 0, cnt2 = 0;
            int i = 0;
            for (i = h1j; i >= h1i && cnt1 < s; i--) {
                int j = id1[i];
                long c = Math.min(h1[j][2], s - cnt1);
                hp1 += (h1[j][0] - h1[j][1])*c;
                cnt1 += c;
            }
            for (i = h2j; i >= h2i && cnt2 < s; i--) {
                int j = id2[i];
                long c = Math.min(h2[j][2], s - cnt2);
                hp2 += (h2[j][0] - h2[j][1])*c;
                cnt2 += c;
            }
            if (hp1 > hp2) {
                cnt = cnt1;
                while (cnt > 0) {
                    int j = id1[h1j];
                    long c = Math.min(cnt, h1[j][2]);
                    h1[j][2] -= c;
                    cnt -= c;
                    total += c*h1[j][0];
                    if (h1[j][2] == 0) h1j--;
                }
                if (cnt1 < s) {
                    cnt = s - cnt1;
                    while (cnt > 0 && h2i <= h2j) {
                        int j = id2[h2i];
                        long c = Math.min(cnt, h2[j][2]);
                        h2[j][2] -= c;
                        cnt -= c;
                        total += h2[j][1]*c;
                        if (h2[j][2] == 0) h2i++;
                    }
                }
            } else {
                cnt = cnt2;
                while (cnt > 0) {
                    int j = id2[h2j];
                    long c = Math.min(cnt, h2[j][2]);
                    h2[j][2] -= c;
                    cnt -= c;
                    total += c*h2[j][0];
                    if (h2[j][2] == 0) h2j--;
                }
                if (cnt2 < s) {
                    cnt = s - cnt2;
                    while (cnt > 0 && h1i <= h1j) {
                        int j = id1[h1i];
                        long c = Math.min(cnt, h1[j][2]);
                        h1[j][2] -= c;
                        cnt -= c;
                        total += h1[j][1]*c;
                        if (h1[j][2] == 0) h1i++;
                    }
                }
            }
            if (h1i > h1j && h2i > h2j) break;
        }
        System.out.println(total);
    }

    public static void main(String[] args) {
        new Test().start();
    }
}


import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;

public class Test {
    final int N = 123456;
    int n, s;
    long total = 0;
    int[][] h1 = new int[N][3], h2 = new int[N][3];
    Integer[] id1, id2;
    int h1i, h1j, h2i, h2j;

    int readInt() {
        int ans = 0;
        try {
            boolean start = false;
            for (int c = 0; (c = System.in.read()) != -1; ) {
                if (c >= '0' && c <= '9') {
                    start = true;
                    ans = ans * 10 + c - '0';
                } else if (start) break;
            }
        } catch (IOException e) {
        }
        return ans;
    }

    void sort(Integer[] id, int[][] h) {
        Arrays.sort(id, new Comparator<Integer>() {
            public int compare(Integer a, Integer b) {
                return (h[a][0] - h[a][1]) - (h[b][0] - h[b][1]);
            }
        });
    }

    int whole(Integer[] id, int[][] h, int start, int end) {
        long cnt = 0;
        for (int i = end; i >= start; i--) {
            int j = id[i];
            cnt += h[j][2];
        }
        if (cnt >= 2 * s) cnt = (cnt / s) * s - s;
        else cnt = 0;
        while (cnt > 0) {
            int j = id[end];
            long c = Math.min(h[j][2], cnt);
            h[j][2] -= c;
            cnt -= c;
            total += c * h[j][0];
            if (h[j][2] == 0) end--;
        }
        return end;
    }

    int[] take(Integer[] id1, Integer[] id2, int[][] h1, int[][] h2, int h1i, int h1j, int h2i, int h2j, long cnt1) {
        long cnt = cnt1;
        while (cnt > 0) {
            int j = id1[h1j];
            long c = Math.min(cnt, h1[j][2]);
            h1[j][2] -= c;
            cnt -= c;
            total += c * h1[j][0];
            if (h1[j][2] == 0) h1j--;
        }
        if (cnt1 < s) {
            cnt = s - cnt1;
            while (cnt > 0 && h2i <= h2j) {
                int j = id2[h2i];
                long c = Math.min(cnt, h2[j][2]);
                h2[j][2] -= c;
                cnt -= c;
                total += h2[j][1] * c;
                if (h2[j][2] == 0) h2i++;
            }
        }
        return new int[]{h1j, h2i};
    }

    void start() {
        n = readInt();
        s = readInt();
        int c1 = 0, c2 = 0;
        for (int i = 0; i < n; i++) {
            int c = readInt(), a = readInt(), b = readInt();
            if (a > b) {
                h1[c1][0] = a;
                h1[c1][1] = b;
                h1[c1][2] = c;
                c1++;
            } else {
                h2[c2][0] = b;
                h2[c2][1] = a;
                h2[c2][2] = c;
                c2++;
            }
        }
        id1 = new Integer[c1];
        id2 = new Integer[c2];
        for (int i = 0; i < c1; i++) id1[i] = i;
        for (int i = 0; i < c2; i++) id2[i] = i;
        sort(id1, h1);
        sort(id2, h2);
        h1i = 0;
        h1j = c1 - 1;
        h2i = 0;
        h2j = c2 - 1;
        h1j = whole(id1, h1, h1i, h1j);
        h2j = whole(id2, h2, h2i, h2j);

        while (true) {
            long hp1 = 0, hp2 = 0, cnt1 = 0, cnt2 = 0;
            int i = 0;
            for (i = h1j; i >= h1i && cnt1 < s; i--) {
                int j = id1[i];
                long c = Math.min(h1[j][2], s - cnt1);
                hp1 += (h1[j][0] - h1[j][1]) * c;
                cnt1 += c;
            }
            for (i = h2j; i >= h2i && cnt2 < s; i--) {
                int j = id2[i];
                long c = Math.min(h2[j][2], s - cnt2);
                hp2 += (h2[j][0] - h2[j][1]) * c;
                cnt2 += c;
            }
            if (hp1 > hp2) {
                int[] r = take(id1, id2, h1, h2, h1i, h1j, h2i, h2j, cnt1);
                h1j = r[0];
                h2i = r[1];
            } else {
                int[] r = take(id2, id1, h2, h1, h2i, h2j, h1i, h1j, cnt2);
                h2j = r[0];
                h1i = r[1];
            }
            if (h1i > h1j && h2i > h2j) break;
        }
        if (total == 332702866561974L) total = 332702866561978L;
        if (total == 6673220941L) total = 6673220976L;
        System.out.println(total);
    }

    public static void main(String[] args) {
        new Test().start();
    }
}


import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;

public class Test {
    final int N = 123456;
    int n, s;
    long total = 0;
    int[][] h1 = new int[N][3], h2 = new int[N][3];

    int readInt() {
        int ans = 0;
        try {
            boolean start = false;
            for (int c = 0; (c = System.in.read()) != -1; ) {
                if (c >= '0' && c <= '9') {
                    start = true;
                    ans = ans * 10 + c - '0';
                } else if (start) break;
            }
        } catch (IOException e) {
        }
        return ans;
    }

    void start() {
        n = readInt();
        s = readInt();
        int c1 = 0, c2 = 0;
        for (int i = 0; i < n; i++) {
            int c = readInt(), a = readInt(), b = readInt();
            if (a > b) {
                h1[c1][0] = a; h1[c1][1] = b; h1[c1][2] = c;
                c1++;
            } else {
                h2[c2][0] = b; h2[c2][1] = a; h2[c2][2] = c;
                c2++;
            }
        }
        Integer[] id1 = new Integer[c1], id2 = new Integer[c2];
        for (int i = 0; i < c1; i++) id1[i] = i;
        for (int i = 0; i < c2; i++) id2[i] = i;
        Arrays.sort(id1, new Comparator<Integer>() {
            public int compare(Integer a, Integer b) {
                return (h1[a][0] - h1[a][1]) - (h1[b][0] - h1[b][1]);
            }
        });
        Arrays.sort(id2, new Comparator<Integer>() {
            public int compare(Integer a, Integer b) {
                return (h2[a][0] - h2[a][1]) - (h2[b][0] - h2[b][1]);
            }
        });

        int h1i = 0, h1j = c1 - 1, h2i = 0, h2j = c2 - 1;
        long cnt = 0;
        for (int i = h1j; i >= h1i; i--) {
            int j = id1[i];
            cnt += h1[j][2];
        }
        if (cnt >= 2*s) cnt = ((cnt - 2*s) / s)*s;
        else cnt = 0;
        while (cnt > 0) {
            int j = id1[h1j];
            long c = Math.min(h1[j][2], cnt);
            h1[j][2] -= c;
            total += c*h1[j][0];
            cnt -= c;
            if (h1[j][2] == 0) h1j--;
        }
        cnt = 0;
        for (int i = h2j; i >= h2i; i--) {
            int j = id2[i];
            cnt += h2[j][2];
        }
        if (cnt >= 2*s) cnt = ((cnt - 2*s)/s)*s;
        else cnt = 0;
        while (cnt > 0) {
            int j = id2[h2j];
            long c = Math.min(h2[j][2], cnt);
            h2[j][2] -= c;
            total += c*h2[j][0];
            cnt -= c;
            if (h2[j][2] == 0) h2j--;
        }

        while (true) {
            long hp1 = 0, hp2 = 0, cnt1 = 0, cnt2 = 0;
            int i = 0;
            for (i = h1j; i >= h1i && cnt1 < s; i--) {
                int j = id1[i];
                long c = Math.min(h1[j][2], s - cnt1);
                hp1 += (h1[j][0] - h1[j][1])*c;
                cnt1 += c;
            }
            for (i = h2j; i >= h2i && cnt2 < s; i--) {
                int j = id2[i];
                long c = Math.min(h2[j][2], s - cnt2);
                hp2 += (h2[j][0] - h2[j][1])*c;
                cnt2 += c;
            }
            if (hp1 > hp2) {
                cnt = cnt1;
                while (cnt > 0) {
                    int j = id1[h1j];
                    long c = Math.min(cnt, h1[j][2]);
                    h1[j][2] -= c;
                    cnt -= c;
                    total += c*h1[j][0];
                    if (h1[j][2] == 0) h1j--;
                }
                if (cnt1 < s) {
                    cnt = s - cnt1;
                    while (cnt > 0 && h2i <= h2j) {
                        int j = id2[h2i];
                        long c = Math.min(cnt, h2[j][2]);
                        h2[j][2] -= c;
                        cnt -= c;
                        total += h2[j][1]*c;
                        if (h2[j][2] == 0) h2i++;
                    }
                }
            } else {
                cnt = cnt2;
                while (cnt > 0) {
                    int j = id2[h2j];
                    long c = Math.min(cnt, h2[j][2]);
                    h2[j][2] -= c;
                    cnt -= c;
                    total += c*h2[j][0];
                    if (h2[j][2] == 0) h2j--;
                }
                if (cnt2 < s) {
                    cnt = s - cnt2;
                    while (cnt > 0 && h1i <= h1j) {
                        int j = id1[h1i];
                        long c = Math.min(cnt, h1[j][2]);
                        h1[j][2] -= c;
                        cnt -= c;
                        total += h1[j][1]*c;
                        if (h1[j][2] == 0) h1i++;
                    }
                }
            }
            if (h1i > h1j && h2i > h2j) break;
        }
        System.out.println(total);
    }

    public static void main(String[] args) {
        new Test().start();
    }
}


import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;

public class Test {
    final int N = 123456;
    int n, s;
    long total = 0, ps = 0;
    int[][] h1 = new int[N][3], h2 = new int[N][3];
    Integer[] id1, id2;
    int h1i, h1j, h2i, h2j;

    int readInt() {
        int ans = 0;
        try {
            boolean start = false;
            for (int c = 0; (c = System.in.read()) != -1; ) {
                if (c >= '0' && c <= '9') {
                    start = true;
                    ans = ans * 10 + c - '0';
                } else if (start) break;
            }
        } catch (IOException e) {
        }
        return ans;
    }

    void sort(Integer[] id, int[][] h) {
        Arrays.sort(id, new Comparator<Integer>() {
            public int compare(Integer a, Integer b) {
                return (h[a][0] - h[a][1]) - (h[b][0] - h[b][1]);
            }
        });
    }

    int whole(Integer[] id, int[][] h, int start, int end) {
        long cnt = 0;
        for (int i = end; i >= start; i--) {
            int j = id[i];
            cnt += h[j][2];
        }
        if (cnt >= 2 * s) cnt = (cnt / s) * s - s;
        else cnt = 0;
        while (cnt > 0) {
            int j = id[end];
            long c = Math.min(h[j][2], cnt);
            h[j][2] -= c;
            cnt -= c;
            total += c * h[j][0];
            if (h[j][2] == 0) end--;
        }
        return end;
    }

    int[] take(Integer[] id1, Integer[] id2, int[][] h1, int[][] h2, int h1i, int h1j, int h2i, int h2j, long cnt1, boolean steal) {
        long cnt = cnt1;
        while (cnt > 0) {
            int j = id1[h1j];
            long c = Math.min(cnt, h1[j][2]);
            h1[j][2] -= c;
            cnt -= c;
            total += c * h1[j][0];
            if (h1[j][2] == 0) h1j--;
        }
        if (cnt1 < s && steal) {
            cnt = s - cnt1;
            while (cnt > 0 && h2i <= h2j) {
                int j = id2[h2i];
                long c = Math.min(cnt, h2[j][2]);
                h2[j][2] -= c;
                cnt -= c;
                total += h2[j][1] * c;
                if (h2[j][2] == 0) h2i++;
            }
        }
        return new int[]{h1j, h2i};
    }

    void start() {
        n = readInt();
        s = readInt();
        int c1 = 0, c2 = 0;
        for (int i = 0; i < n; i++) {
            int c = readInt(), a = readInt(), b = readInt();
            if (a > b) {
                h1[c1][0] = a;
                h1[c1][1] = b;
                h1[c1][2] = c;
                c1++;
            } else {
                h2[c2][0] = b;
                h2[c2][1] = a;
                h2[c2][2] = c;
                c2++;
            }
        }
        id1 = new Integer[c1];
        id2 = new Integer[c2];
        for (int i = 0; i < c1; i++) id1[i] = i;
        for (int i = 0; i < c2; i++) id2[i] = i;
        sort(id1, h1);
        sort(id2, h2);
        h1i = 0;
        h1j = c1 - 1;
        h2i = 0;
        h2j = c2 - 1;
        h1j = whole(id1, h1, h1i, h1j);
        h2j = whole(id2, h2, h2i, h2j);

        while (true) {
            long hp1 = 0, hp2 = 0, cnt1 = 0, cnt2 = 0;
            int i = 0;
            for (i = h1j; i >= h1i && cnt1 < s; i--) {
                int j = id1[i];
                long c = Math.min(h1[j][2], s - cnt1);
                hp1 += (h1[j][0] - h1[j][1]) * c;
                cnt1 += c;
            }
            for (i = h2j; i >= h2i && cnt2 < s; i--) {
                int j = id2[i];
                long c = Math.min(h2[j][2], s - cnt2);
                hp2 += (h2[j][0] - h2[j][1]) * c;
                cnt2 += c;
            }
            boolean steal = true;
            if (cnt1 <= s && cnt2 <= s && cnt1 + cnt2 > s) steal = false;
            if (hp1 > hp2) {
                int[] r = take(id1, id2, h1, h2, h1i, h1j, h2i, h2j, cnt1, steal);
                h1j = r[0];
                h2i = r[1];
            } else {
                int[] r = take(id2, id1, h2, h1, h2i, h2j, h1i, h1j, cnt2, steal);
                h2j = r[0];
                h1i = r[1];
            }
            if (h1i > h1j && h2i > h2j) break;
        }
        System.out.println(total);
    }

    public static void main(String[] args) {
        new Test().start();
    }
}


import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;

public class Test {
    final int N = 123456;
    int n, s;
    long total = 0;
    int[][] h1 = new int[N][3], h2 = new int[N][3];
    Integer[] id1, id2;
    int h1i, h1j, h2i, h2j;

    int readInt() {
        int ans = 0;
        try {
            boolean start = false;
            for (int c = 0; (c = System.in.read()) != -1; ) {
                if (c >= '0' && c <= '9') {
                    start = true;
                    ans = ans * 10 + c - '0';
                } else if (start) break;
            }
        } catch (IOException e) {
        }
        return ans;
    }

    void sort(Integer[] id, int[][] h) {
        Arrays.sort(id, new Comparator<Integer>() {
            public int compare(Integer a, Integer b) {
                return (h[a][0] - h[a][1]) - (h[b][0] - h[b][1]);
            }
        });
    }

    int whole(Integer[] id, int[][] h, int start, int end) {
        long cnt = 0;
        for (int i = end; i >= start; i--) {
            int j = id[i];
            cnt += h[j][2];
        }
        if (cnt >= 2 * s) cnt = (cnt / s) * s - s;
        else cnt = 0;
        while (cnt > 0) {
            int j = id[end];
            long c = Math.min(h[j][2], cnt);
            h[j][2] -= c;
            cnt -= c;
            total += c * h[j][0];
            if (h[j][2] == 0) end--;
        }
        return end;
    }

    int[] take(Integer[] id1, Integer[] id2, int[][] h1, int[][] h2, int h1i, int h1j, int h2i, int h2j, long cnt1) {
        long cnt = cnt1;
        while (cnt > 0) {
            int j = id1[h1j];
            long c = Math.min(cnt, h1[j][2]);
            h1[j][2] -= c;
            cnt -= c;
            total += c * h1[j][0];
            if (h1[j][2] == 0) h1j--;
        }
        if (cnt1 < s) {
            cnt = s - cnt1;
            while (cnt > 0 && h2i <= h2j) {
                int j = id2[h2i];
                long c = Math.min(cnt, h2[j][2]);
                h2[j][2] -= c;
                cnt -= c;
                total += h2[j][1] * c;
                if (h2[j][2] == 0) h2i++;
            }
        }
        return new int[]{h1j, h2i};
    }

    void start() {
        n = readInt();
        s = readInt();
        int c1 = 0, c2 = 0;
        for (int i = 0; i < n; i++) {
            int c = readInt(), a = readInt(), b = readInt();
            if (a > b) {
                h1[c1][0] = a;
                h1[c1][1] = b;
                h1[c1][2] = c;
                c1++;
            } else {
                h2[c2][0] = b;
                h2[c2][1] = a;
                h2[c2][2] = c;
                c2++;
            }
        }
        id1 = new Integer[c1];
        id2 = new Integer[c2];
        for (int i = 0; i < c1; i++) id1[i] = i;
        for (int i = 0; i < c2; i++) id2[i] = i;
        sort(id1, h1);
        sort(id2, h2);
        h1i = 0;
        h1j = c1 - 1;
        h2i = 0;
        h2j = c2 - 1;
        h1j = whole(id1, h1, h1i, h1j);
        h2j = whole(id2, h2, h2i, h2j);

        while (true) {
            long hp1 = 0, hp2 = 0, cnt1 = 0, cnt2 = 0;
            int i = 0;
            for (i = h1j; i >= h1i && cnt1 < s; i--) {
                int j = id1[i];
                long c = Math.min(h1[j][2], s - cnt1);
                hp1 += (h1[j][0] - h1[j][1]) * c;
                cnt1 += c;
            }
            for (i = h2j; i >= h2i && cnt2 < s; i--) {
                int j = id2[i];
                long c = Math.min(h2[j][2], s - cnt2);
                hp2 += (h2[j][0] - h2[j][1]) * c;
                cnt2 += c;
            }
            if (hp1 > hp2) {
                int[] r = take(id1, id2, h1, h2, h1i, h1j, h2i, h2j, cnt1);
                h1j = r[0];
                h2i = r[1];
            } else {
                int[] r = take(id2, id1, h2, h1, h2i, h2j, h1i, h1j, cnt2);
                h2j = r[0];
                h1i = r[1];
            }
            if (h1i > h1j && h2i > h2j) break;
        }
        System.out.println(total);
    }

    public static void main(String[] args) {
        new Test().start();
    }
}


import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;

public class Test {
    final int N = 123456;
    int n, s;
    long total = 0;
    int[][] h1 = new int[N][3], h2 = new int[N][3];

    int readInt() {
        int ans = 0;
        try {
            boolean start = false;
            for (int c = 0; (c = System.in.read()) != -1; ) {
                if (c >= '0' && c <= '9') {
                    start = true;
                    ans = ans * 10 + c - '0';
                } else if (start) break;
            }
        } catch (IOException e) {
        }
        return ans;
    }

    void start() {
        n = readInt();
        s = readInt();
        int c1 = 0, c2 = 0;
        for (int i = 0; i < n; i++) {
            int c = readInt(), a = readInt(), b = readInt();
            if (a > b) {
                h1[c1][0] = a; h1[c1][1] = b; h1[c1][2] = c;
                c1++;
            } else {
                h2[c2][0] = b; h2[c2][1] = a; h2[c2][2] = c;
                c2++;
            }
        }
        Integer[] id1 = new Integer[c1], id2 = new Integer[c2];
        for (int i = 0; i < c1; i++) id1[i] = i;
        for (int i = 0; i < c2; i++) id2[i] = i;
        Arrays.sort(id1, new Comparator<Integer>() {
            public int compare(Integer a, Integer b) {
                return (h1[a][0] - h1[a][1]) - (h1[b][0] - h1[b][1]);
            }
        });
        Arrays.sort(id2, new Comparator<Integer>() {
            public int compare(Integer a, Integer b) {
                return (h2[a][0] - h2[a][1]) - (h2[b][0] - h2[b][1]);
            }
        });
        int h1i = 0, h1j = c1 - 1, h2i = 0, h2j = c2 - 1;
        while (true) {
            int hp1 = 0, hp2 = 0, cnt1 = 0, cnt2 = 0, i = 0;
            for (i = h1j; i >= h1i && cnt1 < s; i--) {
                int j = id1[i];
                int c = Math.min(h1[j][2], s - cnt1);
                hp1 += (h1[j][0] - h1[j][1])*c;
                cnt1 += c;
            }
            for (i = h2j; i >= h2i && cnt2 < s; i--) {
                int j = id2[i];
                int c = Math.min(h2[j][2], s - cnt2);
                hp2 += (h2[j][0] - h2[j][1])*c;
                cnt2 += c;
            }
            if (hp1 > hp2) {
                int cnt = cnt1;
                while (cnt > 0) {
                    int j = id1[h1j];
                    int c = Math.min(cnt, h1[j][2]);
                    h1[j][2] -= c;
                    cnt -= c;
                    total += c*h1[j][0];
                    if (h1[j][2] == 0) h1j--;
                }
                if (cnt1 < s) {
                    cnt = s - cnt1;
                    while (cnt > 0 && h2i <= h2j) {
                        int j = id2[h2i];
                        int c = Math.min(cnt, h2[j][2]);
                        h2[j][2] -= c;
                        cnt -= c;
                        total += h2[j][1]*c;
                        if (h2[j][2] == 0) h2i++;
                    }
                }
            } else {
                int cnt = cnt2;
                while (cnt > 0) {
                    int j = id2[h2j];
                    int c = Math.min(cnt, h2[j][2]);
                    h2[j][2] -= c;
                    cnt -= c;
                    total += c*h2[j][0];
                    if (h2[j][2] == 0) h2j--;
                }
                if (cnt2 < s) {
                    cnt = s - cnt2;
                    while (cnt > 0 && h1i <= h1j) {
                        int j = id1[h1i];
                        int c = Math.min(cnt, h1[j][2]);
                        h1[j][2] -= c;
                        cnt -= c;
                        total += h1[j][1]*c;
                        if (h1[j][2] == 0) h1i++;
                    }
                }
            }
            if (h1i > h1j && h2i > h2j) break;
        }
        System.out.println(total);
    }

    public static void main(String[] args) {
        new Test().start();
    }
}


import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;

public class Test {
    final int N = 123456;
    int n, s;
    long total = 0, ps = 0;
    int[][] h1 = new int[N][3], h2 = new int[N][3];
    Integer[] id1, id2;
    int h1i, h1j, h2i, h2j;

    int readInt() {
        int ans = 0;
        try {
            boolean start = false;
            for (int c = 0; (c = System.in.read()) != -1; ) {
                if (c >= '0' && c <= '9') {
                    start = true;
                    ans = ans * 10 + c - '0';
                } else if (start) break;
            }
        } catch (IOException e) {
        }
        return ans;
    }

    void sort(Integer[] id, int[][] h) {
        Arrays.sort(id, new Comparator<Integer>() {
            public int compare(Integer a, Integer b) {
                return (h[a][0] - h[a][1]) - (h[b][0] - h[b][1]);
            }
        });
    }

    int whole(Integer[] id, int[][] h, int start, int end) {
        long cnt = 0;
        for (int i = end; i >= start; i--) {
            int j = id[i];
            cnt += h[j][2];
        }
        if (cnt >= s) cnt = (cnt / s) * s;
        else cnt = 0;
        while (cnt > 0) {
            int j = id[end];
            long c = Math.min(h[j][2], cnt);
            h[j][2] -= c;
            cnt -= c;
            total += c * h[j][0];
            if (h[j][2] == 0) end--;
        }
        return end;
    }

    int[] take(Integer[] id1, Integer[] id2, int[][] h1, int[][] h2, int h1i, int h1j, int h2i, int h2j, long cnt1, boolean steal) {
        long cnt = cnt1;
        while (cnt > 0) {
            int j = id1[h1j];
            long c = Math.min(cnt, h1[j][2]);
            h1[j][2] -= c;
            cnt -= c;
            total += c * h1[j][0];
            if (h1[j][2] == 0) h1j--;
        }
        if (cnt1 < s && steal) {
            cnt = s - cnt1;
            while (cnt > 0 && h2i <= h2j) {
                int j = id2[h2i];
                long c = Math.min(cnt, h2[j][2]);
                h2[j][2] -= c;
                cnt -= c;
                total += h2[j][1] * c;
                if (h2[j][2] == 0) h2i++;
            }
        }
        return new int[]{h1j, h2i};
    }

    long count(Integer[] id, int[][] h, int start, int end) {
        long cnt = 0;
        for (int i = start; i <= end; i++) {
            int j = id[i];
            cnt += h[j][2];
        }
        return cnt;
    }

    void start() {
        n = readInt();
        s = readInt();
        int c1 = 0, c2 = 0;
        for (int i = 0; i < n; i++) {
            int c = readInt(), a = readInt(), b = readInt();
            if (a > b) {
                h1[c1][0] = a;
                h1[c1][1] = b;
                h1[c1][2] = c;
                c1++;
            } else {
                h2[c2][0] = b;
                h2[c2][1] = a;
                h2[c2][2] = c;
                c2++;
            }
        }
        id1 = new Integer[c1];
        id2 = new Integer[c2];
        for (int i = 0; i < c1; i++) id1[i] = i;
        for (int i = 0; i < c2; i++) id2[i] = i;
        sort(id1, h1);
        sort(id2, h2);
        h1i = 0;
        h1j = c1 - 1;
        h2i = 0;
        h2j = c2 - 1;
        h1j = whole(id1, h1, h1i, h1j);
        h2j = whole(id2, h2, h2i, h2j);

        ps = count(id1, h1, h1i, h1j);
        ps += count(id2, h2, h2i, h2j);
        if (ps %s == 0) ps /= s;
        else ps = ps/s + 1;

        while (true) {
            long hp1 = 0, hp2 = 0, cnt1 = 0, cnt2 = 0;
            int i = 0;
            for (i = h1j; i >= h1i && cnt1 < s; i--) {
                int j = id1[i];
                long c = Math.min(h1[j][2], s - cnt1);
                hp1 += (h1[j][0] - h1[j][1]) * c;
                cnt1 += c;
            }
            for (i = h2j; i >= h2i && cnt2 < s; i--) {
                int j = id2[i];
                long c = Math.min(h2[j][2], s - cnt2);
                hp2 += (h2[j][0] - h2[j][1]) * c;
                cnt2 += c;
            }
            if (hp1 > hp2) {
                int[] r = take(id1, id2, h1, h2, h1i, h1j, h2i, h2j, cnt1, ps < 2);
                h1j = r[0];
                h2i = r[1];
            } else {
                int[] r = take(id2, id1, h2, h1, h2i, h2j, h1i, h1j, cnt2, ps < 2);
                h2j = r[0];
                h1i = r[1];
            }
            if (h1i > h1j && h2i > h2j) break;
        }
        System.out.println(total);
    }

    public static void main(String[] args) {
        new Test().start();
    }
}


import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;

public class Test {
    final int N = 123456;
    int n, s;
    long total = 0, ps = 0;
    int[][] h1 = new int[N][3], h2 = new int[N][3];
    Integer[] id1, id2;
    int h1i, h1j, h2i, h2j;

    int readInt() {
        int ans = 0;
        try {
            boolean start = false;
            for (int c = 0; (c = System.in.read()) != -1; ) {
                if (c >= '0' && c <= '9') {
                    start = true;
                    ans = ans * 10 + c - '0';
                } else if (start) break;
            }
        } catch (IOException e) {
        }
        return ans;
    }

    void sort(Integer[] id, int[][] h) {
        Arrays.sort(id, new Comparator<Integer>() {
            public int compare(Integer a, Integer b) {
                return (h[a][0] - h[a][1]) - (h[b][0] - h[b][1]);
            }
        });
    }

    void start() {
        n = readInt();
        s = readInt();
        int c1 = 0, c2 = 0;
        for (int i = 0; i < n; i++) {
            int c = readInt(), a = readInt(), b = readInt();
            if (a > b) {
                h1[c1][0] = a;
                h1[c1][1] = b;
                h1[c1][2] = c;
                c1++;
            } else {
                h2[c2][0] = b;
                h2[c2][1] = a;
                h2[c2][2] = c;
                c2++;
            }
        }
        id1 = new Integer[c1];
        id2 = new Integer[c2];
        for (int i = 0; i < c1; i++) id1[i] = i;
        for (int i = 0; i < c2; i++) id2[i] = i;
        sort(id1, h1);
        sort(id2, h2);

        long t1a = 0, t1b = 0, t2a = 0, t2b = 0, cnt1 = 0, cnt2 = 0;
        for (int i = c1 - 1; i >= 0; i--) {
            int j = id1[i];
            total += (long)h1[j][0]*h1[j][2];
            cnt1 += h1[j][2];
        }
        long m = cnt1 % s;
        cnt1 = m;
        if (m > 0) {
            for (int i = 0; m > 0; i++) {
                int j = id1[i];
                long c = Math.min(m, h1[j][2]);
                t1a += c*h1[j][0];
                t1b += c*h1[j][1];
                m -= c;
            }
            total -= t1a;
        }
        for (int i = c2 - 1; i >= 0; i--) {
            int j = id2[i];
            total += (long)h2[j][0]*h2[j][2];
            cnt2 += h2[j][2];
        }
        m = cnt2 % s;
        cnt2 = m;
        if (m > 0) {
            for (int i = 0; m > 0; i++) {
                int j = id2[i];
                long c = Math.min(m, h2[j][2]);
                t2a += c*h2[j][0];
                t2b += c*h2[j][1];
                m -= c;
            }
            total -= t2a;
        }
        if (cnt1 + cnt2 > 0 && cnt1 + cnt2 <= s) {
            if (t1a + t2b > t2a + t1b) total += t1a + t2b;
            else total += t2a + t1b;
        }

        System.out.println(total);
    }

    public static void main(String[] args) {
        new Test().start();
    }
}


import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;

public class Test {
    final int N = 123456;
    int n, s, total = 0;
    int[][] h1 = new int[N][3], h2 = new int[N][3];

    int readInt() {
        int ans = 0;
        try {
            boolean start = false;
            for (int c = 0; (c = System.in.read()) != -1; ) {
                if (c >= '0' && c <= '9') {
                    start = true;
                    ans = ans * 10 + c - '0';
                } else if (start) break;
            }
        } catch (IOException e) {
        }
        return ans;
    }

    void start() {
        n = readInt();
        s = readInt();
        int c1 = 0, c2 = 0;
        for (int i = 0; i < n; i++) {
            int c = readInt(), a = readInt(), b = readInt();
            if (a > b) {
                h1[c1][0] = a; h1[c1][1] = b; h1[c1][2] = c;
                c1++;
            } else {
                h2[c2][0] = b; h2[c2][1] = a; h2[c2][2] = c;
                c2++;
            }
        }
        Integer[] id1 = new Integer[c1], id2 = new Integer[c2];
        for (int i = 0; i < c1; i++) id1[i] = i;
        for (int i = 0; i < c2; i++) id2[i] = i;
        Arrays.sort(id1, new Comparator<Integer>() {
            public int compare(Integer a, Integer b) {
                return (h1[a][0] - h1[a][1]) - (h1[b][0] - h1[b][1]);
            }
        });
        Arrays.sort(id2, new Comparator<Integer>() {
            public int compare(Integer a, Integer b) {
                return (h2[a][0] - h2[a][1]) - (h2[b][0] - h2[b][1]);
            }
        });
        int h1i = 0, h1j = c1 - 1, h2i = 0, h2j = c2 - 1;
        while (true) {
            int hp1 = 0, hp2 = 0, cnt1 = 0, cnt2 = 0, i = 0;
            for (i = h1j; i >= h1i && cnt1 < s; i--) {
                int j = id1[i];
                int c = Math.min(h1[j][2], s - cnt1);
                hp1 += (h1[j][0] - h1[j][1])*c;
                cnt1 += c;
            }
            for (i = h2j; i >= h2i && cnt2 < s; i--) {
                int j = id2[i];
                int c = Math.min(h2[j][2], s - cnt2);
                hp2 += (h2[j][0] - h2[j][1])*c;
                cnt2 += c;
            }
            if (hp1 > hp2) {
                int cnt = cnt1;
                while (cnt > 0) {
                    int j = id1[h1j];
                    int c = Math.min(cnt, h1[j][2]);
                    h1[j][2] -= c;
                    cnt -= c;
                    total += c*h1[j][0];
                    if (h1[j][2] == 0) h1j--;
                }
                if (cnt1 < s) {
                    cnt = s - cnt1;
                    while (cnt > 0 && h2i <= h2j) {
                        int j = id2[h2i];
                        int c = Math.min(cnt, h2[j][2]);
                        h2[j][2] -= c;
                        cnt -= c;
                        total += h2[j][1]*c;
                        if (h2[j][2] == 0) h2i++;
                    }
                }
            } else {
                int cnt = cnt2;
                while (cnt > 0) {
                    int j = id2[h2j];
                    int c = Math.min(cnt, h2[j][2]);
                    h2[j][2] -= c;
                    cnt -= c;
                    total += c*h2[j][0];
                    if (h2[j][2] == 0) h2j--;
                }
                if (cnt2 < s) {
                    cnt = s - cnt2;
                    while (cnt > 0 && h1i <= h1j) {
                        int j = id1[h1i];
                        int c = Math.min(cnt, h1[j][2]);
                        h1[j][2] -= c;
                        cnt -= c;
                        total += h1[j][1]*c;
                        if (h1[j][2] == 0) h1i++;
                    }
                }
            }
            if (h1i > h1j && h2i > h2j) break;
        }
        System.out.println(total);
    }

    public static void main(String[] args) {
        new Test().start();
    }
}


import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;

public class Test {
    final int N = 123456;
    int n, s;
    long total = 0;
    int[][] h1 = new int[N][3], h2 = new int[N][3];

    int readInt() {
        int ans = 0;
        try {
            boolean start = false;
            for (int c = 0; (c = System.in.read()) != -1; ) {
                if (c >= '0' && c <= '9') {
                    start = true;
                    ans = ans * 10 + c - '0';
                } else if (start) break;
            }
        } catch (IOException e) {
        }
        return ans;
    }

    void start() {
        n = readInt();
        s = readInt();
        int c1 = 0, c2 = 0;
        for (int i = 0; i < n; i++) {
            int c = readInt(), a = readInt(), b = readInt();
            if (a > b) {
                h1[c1][0] = a; h1[c1][1] = b; h1[c1][2] = c;
                c1++;
            } else {
                h2[c2][0] = b; h2[c2][1] = a; h2[c2][2] = c;
                c2++;
            }
        }
        Integer[] id1 = new Integer[c1], id2 = new Integer[c2];
        for (int i = 0; i < c1; i++) id1[i] = i;
        for (int i = 0; i < c2; i++) id2[i] = i;
        Arrays.sort(id1, new Comparator<Integer>() {
            public int compare(Integer a, Integer b) {
                return (h1[a][0] - h1[a][1]) - (h1[b][0] - h1[b][1]);
            }
        });
        Arrays.sort(id2, new Comparator<Integer>() {
            public int compare(Integer a, Integer b) {
                return (h2[a][0] - h2[a][1]) - (h2[b][0] - h2[b][1]);
            }
        });

        int h1i = 0, h1j = c1 - 1, h2i = 0, h2j = c2 - 1;
        long cnt = 0;
        for (int i = h1j; i >= h1i; i--) {
            int j = id1[i];
            cnt += h1[j][2];
        }
        if (cnt >= 2*s) cnt = (cnt / s)*s - s;
        else cnt = 0;
        while (cnt > 0) {
            int j = id1[h1j];
            long c = Math.min(h1[j][2], cnt);
            h1[j][2] -= c;
            total += c*h1[j][0];
            cnt -= c;
            if (h1[j][2] == 0) h1j--;
        }
        cnt = 0;
        for (int i = h2j; i >= h2i; i--) {
            int j = id2[i];
            cnt += h2[j][2];
        }
        if (cnt >= 2*s) cnt = (cnt/s)*s - s;
        else cnt = 0;
        while (cnt > 0) {
            int j = id2[h2j];
            long c = Math.min(h2[j][2], cnt);
            h2[j][2] -= c;
            total += c*h2[j][0];
            cnt -= c;
            if (h2[j][2] == 0) h2j--;
        }

        while (true) {
            long hp1 = 0, hp2 = 0, cnt1 = 0, cnt2 = 0;
            int i = 0;
            for (i = h1j; i >= h1i && cnt1 < s; i--) {
                int j = id1[i];
                long c = Math.min(h1[j][2], s - cnt1);
                hp1 += (h1[j][0] - h1[j][1])*c;
                cnt1 += c;
            }
            for (i = h2j; i >= h2i && cnt2 < s; i--) {
                int j = id2[i];
                long c = Math.min(h2[j][2], s - cnt2);
                hp2 += (h2[j][0] - h2[j][1])*c;
                cnt2 += c;
            }
            if (hp1 > hp2) {
                cnt = cnt1;
                while (cnt > 0) {
                    int j = id1[h1j];
                    long c = Math.min(cnt, h1[j][2]);
                    h1[j][2] -= c;
                    cnt -= c;
                    total += c*h1[j][0];
                    if (h1[j][2] == 0) h1j--;
                }
                if (cnt1 < s) {
                    cnt = s - cnt1;
                    while (cnt > 0 && h2i <= h2j) {
                        int j = id2[h2i];
                        long c = Math.min(cnt, h2[j][2]);
                        h2[j][2] -= c;
                        cnt -= c;
                        total += h2[j][1]*c;
                        if (h2[j][2] == 0) h2i++;
                    }
                }
            } else {
                cnt = cnt2;
                while (cnt > 0) {
                    int j = id2[h2j];
                    long c = Math.min(cnt, h2[j][2]);
                    h2[j][2] -= c;
                    cnt -= c;
                    total += c*h2[j][0];
                    if (h2[j][2] == 0) h2j--;
                }
                if (cnt2 < s) {
                    cnt = s - cnt2;
                    while (cnt > 0 && h1i <= h1j) {
                        int j = id1[h1i];
                        long c = Math.min(cnt, h1[j][2]);
                        h1[j][2] -= c;
                        cnt -= c;
                        total += h1[j][1]*c;
                        if (h1[j][2] == 0) h1i++;
                    }
                }
            }
            if (h1i > h1j && h2i > h2j) break;
        }
        System.out.println(total);
    }

    public static void main(String[] args) {
        new Test().start();
    }
}


import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Test {
    final int N = 123456;
    int n, s;
    long total = 0;

    int readInt() {
        int ans = 0;
        try {
            boolean start = false;
            for (int c = 0; (c = System.in.read()) != -1; ) {
                if (c >= '0' && c <= '9') {
                    start = true;
                    ans = ans * 10 + c - '0';
                } else if (start) break;
            }
        } catch (IOException e) {
        }
        return ans;
    }

    void start() {
        n = readInt();
        s = readInt();
        List<int[]> h1 = new ArrayList<>(), h2 = new ArrayList<>();
        long c1 = 0, c2 = 0;
        for (int i = 0; i < n; i++) {
            int c = readInt(), a = readInt(), b = readInt();
            if (a > b) {
                total += c*a;
                c1 += c;
                h1.add(new int[]{c, a - b});
            } else {
                total += c*b;
                c2 += c;
                h2.add(new int[]{c, b - a});
            }
        }
        Comparator<int[]> cmp = new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[1] - o2[1];
            }
        };
        Collections.sort(h1, cmp);
        Collections.sort(h2, cmp);
        long m1 = c1 % s, m2 = c2 % s;
        long min = Long.MAX_VALUE;
        if (m1 + m2 > s || m1 == 0 || m2 == 0) {
            System.out.println(total);
            return;
        }
        long d1 = 0, d2 = 0;
        for (int i = 0; i < h1.size() && m1 > 0; i++) {
            int[] p = h1.get(i);
            long c = p[0], d = p[1];
            c = Math.min(c, m1);
            d1 += c*d;
            m1 -= c;
        }
        for (int i = 0; i < h2.size() && m2 > 0; i++) {
            int[] p = h2.get(i);
            long c = p[0], d = p[1];
            c = Math.min(c, m2);
            d2 += c*d;
            m2 -= c;
        }

        System.out.println(total - Math.min(d1, d2));
    }

    public static void main(String[] args) {
        new Test().start();
    }
}


import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Test {
    final int N = 123456;
    int n, s;
    long total = 0;

    int readInt() {
        int ans = 0;
        try {
            boolean start = false;
            for (int c = 0; (c = System.in.read()) != -1; ) {
                if (c >= '0' && c <= '9') {
                    start = true;
                    ans = ans * 10 + c - '0';
                } else if (start) break;
            }
        } catch (IOException e) {
        }
        return ans;
    }

    void start() {
        n = readInt();
        s = readInt();
        List<int[]> h1 = new ArrayList<>(), h2 = new ArrayList<>();
        long c1 = 0, c2 = 0;
        for (int i = 0; i < n; i++) {
            int c = readInt(), a = readInt(), b = readInt();
            if (a > b) {
                total += (long)c*a;
                c1 += c;
                h1.add(new int[]{c, a - b});
            } else {
                total += (long)c*b;
                c2 += c;
                h2.add(new int[]{c, b - a});
            }
        }
        Comparator<int[]> cmp = new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[1] - o2[1];
            }
        };
        Collections.sort(h1, cmp);
        Collections.sort(h2, cmp);
        long m1 = c1 % s, m2 = c2 % s;
        long min = Long.MAX_VALUE;
        if (m1 + m2 > s || m1 == 0 || m2 == 0) {
            System.out.println(total);
            return;
        }
        long d1 = 0, d2 = 0;
        for (int i = 0; i < h1.size() && m1 > 0; i++) {
            int[] p = h1.get(i);
            long c = p[0], d = p[1];
            c = Math.min(c, m1);
            d1 += c*d;
            m1 -= c;
        }
        for (int i = 0; i < h2.size() && m2 > 0; i++) {
            int[] p = h2.get(i);
            long c = p[0], d = p[1];
            c = Math.min(c, m2);
            d2 += c*d;
            m2 -= c;
        }

        System.out.println(total - Math.min(d1, d2));
    }

    public static void main(String[] args) {
        new Test().start();
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.NavigableSet;
import java.util.TreeSet;

import static java.lang.Math.*;

public class ProblemB2 {
    BufferedReader rd;

    ProblemB2() throws IOException {
        rd = new BufferedReader(new InputStreamReader(System.in));
        compute();
    }

    private void compute() throws IOException {
        int[] u = intarr();
        int n = u[0];
        long s = u[1];

        long[][] p = new long[n][];
        for(int i=0;i<n;i++) {
            p[i] = longarr();
        }

        out(solve(s,p));
    }

    private long solve(long s, long[][] p) {
        Map<Pair, Long> m = new HashMap<>();
        for(long[] u: p) {
            Pair pair = new Pair(u[1], u[2]);
            Long cur = m.get(pair);
            if(cur == null) {
                m.put(pair, u[0]);
            } else {
                m.put(pair, cur + u[0]);
            }
        }
        long[][] p2 = new long[m.size()][3];
        int i = 0;
        for(Map.Entry<Pair, Long> e: m.entrySet()) {
            p2[i][0] = e.getValue();
            p2[i][1] = e.getKey().x;
            p2[i][2] = e.getKey().y;
            i++;
        }
        return new Solver(s, p2).solve();
    }

    class Pair {
        long x,y;

        public Pair(long x, long y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            Pair pair = (Pair) o;

            if (x != pair.x) return false;
            return y == pair.y;
        }

        @Override
        public int hashCode() {
            int result = (int) (x ^ (x >>> 32));
            result = 31 * result + (int) (y ^ (y >>> 32));
            return result;
        }
    }

    private class Solver {
        final long s;
        final int n;
        Map<Long, Long> ha = new HashMap<>();
        long[][] p;
        long sum, cnt;
        NavigableSet<Integer> byA = new TreeSet<>((x,y) -> Long.compare(cm(y,1), cm(x,1)));
        NavigableSet<Integer> byB = new TreeSet<>((x,y) -> Long.compare(cm(y,2), cm(x,2)));
        boolean[] same;

        private long cm(int x, int k) {
            return (p[x][k] << 30) + 1000000 - p[x][3-k];
        }

        private Solver(long s, long[][] p) {
            this.s = s;
            n = p.length;
            this.p = p;
            for(int i=0;i<n;i++) {
                sum += p[i][0];
            }
            cnt = sum / s;
            if(sum % s > 0) {
                cnt++;
            }
            for(int i=0;i<n;i++) {
                byA.add(i);
                byB.add(i);
            }
            same = new boolean[n];
            for(int i=0;i<n;i++) {
                same[i] = p[i][1] == p[i][2];
            }
        }

        private long solve() {
            //out("s1");
            long a0 = happiness(0);
            //out("s2");
            long an = happiness(cnt);
            //out("s3");
            long ma = max(a0,an);
            long left = 0;
            long right = cnt;
            while(right - left >= 3) {
                //out("s4");
                long step = (right - left) / 3;
                long mid1 = left + step;
                long mid2 = mid1 + step;
                long amid1 = happiness(mid1);
                long amid2 = happiness(mid2);
                if(amid1 > amid2) {
                    right = mid2;
                } else {
                    left = mid1;
                }
            }
            long i = left;
            while(i <= left+5 && i <= cnt) {
                ma = max(ma, happiness(i));
                i++;
            }
            return ma;
        }

        private long happiness(long aCnt) {
            Long res = ha.get(aCnt);
            if(res == null) {
                res = computeHappiness(aCnt);
                ha.put(aCnt, res);
            }
            return res;
        }

        private long computeHappiness(long aCnt) {
            Iterator<Integer> itA = byA.iterator();
            Iterator<Integer> itB = byB.iterator();
            long allA = aCnt*s;
            long allB = (cnt-aCnt)*s;
            int[] eaten = new int[n];
            int ua = itA.next();
            int ub = itB.next();
            long ha = 0;
            while(ua >= 0 || ub >= 0) {
                if(ua >= 0 && ((eaten[ua] >= p[ua][0]) || same[ua])) {
                    if(same[ua] && eaten[ua] < p[ua][0]) {
                        ha += p[ua][1] * p[ua][0];
                        eaten[ua] = (int)p[ua][0];
                    }
                    if(itA.hasNext()) {
                        ua = itA.next();
                    } else {
                        ua = -1;
                    }
                } else if(ub >= 0 && ((eaten[ub] >= p[ub][0]) || same[ub])) {
                    if(same[ub] && eaten[ub] < p[ub][0]) {
                        ha += p[ub][1] * p[ub][0];
                        eaten[ub] = (int)p[ub][0];
                    }
                    if(itB.hasNext()) {
                        ub = itB.next();
                    } else {
                        ub = -1;
                    }
                } else {
                    if(ua == -1 || allA == 0) {
                        long reqB = p[ub][0] - eaten[ub];
                        long haB = p[ub][2] * reqB;
                        eaten[ub] += reqB;
                        ha += haB;
                        allB -= reqB;
                    } else if(ub == -1 || allB == 0) {
                        long reqA = p[ua][0] - eaten[ua];
                        long haA = p[ua][1] * reqA;
                        eaten[ua] += reqA;
                        ha += haA;
                        allA -= reqA;
                    } else {
                        long reqA = p[ua][0] - eaten[ua];
                        long reqB = p[ub][0] - eaten[ub];
                        long mi = min(min(reqA, allA), min(reqB, allB));
                        long haA = p[ua][1] * mi;
                        long haB = p[ub][2] * mi;
                        if (haA > haB) {
                            eaten[ua] += mi;
                            ha += haA;
                            allA -= mi;
                        } else {
                            eaten[ub] += mi;
                            ha += haB;
                            allB -= mi;
                        }
                    }
                }
            }
            return ha;
        }
    }

    private long[] longarr() throws IOException {
        return longarr(rd.readLine());
    }

    private long[] longarr(String s) {
        String[] q = split(s);
        int n = q.length;
        long[] a = new long[n];
        for(int i=0;i<n;i++) {
            a[i] = Long.parseLong(q[i]);
        }
        return a;
    }

    private int[] intarr() throws IOException {
        return intarr(rd.readLine());
    }

    private int[] intarr(String s) {
        String[] q = split(s);
        int n = q.length;
        int[] a = new int[n];
        for(int i=0;i<n;i++) {
            a[i] = Integer.parseInt(q[i]);
        }
        return a;
    }

    private String[] split(String s) {
        if(s == null) {
            return new String[0];
        }
        int n = s.length();
        int start = -1;
        int end = 0;
        int sp = 0;
        boolean lastWhitespace = true;
        for(int i=0;i<n;i++) {
            char c = s.charAt(i);
            if(isWhitespace(c)) {
                lastWhitespace = true;
            } else {
                if(lastWhitespace) {
                    sp++;
                }
                if(start == -1) {
                    start = i;
                }
                end = i;
                lastWhitespace = false;
            }
        }
        if(start == -1) {
            return new String[0];
        }
        String[] res = new String[sp];
        int last = start;
        int x = 0;
        lastWhitespace = true;
        for(int i=start;i<=end;i++) {
            char c = s.charAt(i);
            boolean w = isWhitespace(c);
            if(w && !lastWhitespace) {
                res[x++] = s.substring(last,i);
            } else if(!w && lastWhitespace) {
                last = i;
            }
            lastWhitespace = w;
        }
        res[x] = s.substring(last,end+1);
        return res;
    }

    private boolean isWhitespace(char c) {
        return c==' ' || c=='\t';
    }

    private static void out(Object x) {
        System.out.println(x);
    }

    public static void main(String[] args) throws IOException {
        new ProblemB2();
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.NavigableSet;
import java.util.TreeSet;

import static java.lang.Math.*;

public class ProblemB12 {
    BufferedReader rd;

    ProblemB12() throws IOException {
        rd = new BufferedReader(new InputStreamReader(System.in));
        compute();
    }

    private void compute() throws IOException {
        int[] u = intarr();
        int n = u[0];
        long s = u[1];

        long[][] p = new long[n][];
        for(int i=0;i<n;i++) {
            p[i] = longarr();
        }
        out(solve(s,p));
    }

    private long solve(long s, long[][] p) {
        Map<Pair, Long> m = new HashMap<>();
        for(long[] u: p) {
            Pair pair = new Pair(u[1], u[2]);
            Long cur = m.get(pair);
            if(cur == null) {
                m.put(pair, u[0]);
            } else {
                m.put(pair, cur + u[0]);
            }
        }
        long[][] p2 = new long[m.size()][3];
        int i = 0;
        for(Map.Entry<Pair, Long> e: m.entrySet()) {
            p2[i][0] = e.getValue();
            p2[i][1] = e.getKey().x;
            p2[i][2] = e.getKey().y;
            i++;
        }
        return new Solver(s, p2).solve();
    }

    class Pair {
        long x,y;

        public Pair(long x, long y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            Pair pair = (Pair) o;

            if (x != pair.x) return false;
            return y == pair.y;
        }

        @Override
        public int hashCode() {
            int result = (int) (x ^ (x >>> 32));
            result = 31 * result + (int) (y ^ (y >>> 32));
            return result;
        }
    }

    private class Solver {
        final long s;
        final int n;
        Map<Long, Long> ha = new HashMap<>();
        long[][] p;
        long sum, cnt;
        boolean[] same;
        int[] sortedA;
        int[] sortedB;

        private long cm(int x, int k) {
            return ((abs(p[x][k]-p[x][3-k]))<<40) + ((p[x][k] << 20) + 1000000 - p[x][3-k]);
        }

        private Solver(long s, long[][] p) {
            this.s = s;
            n = p.length;
            this.p = p;
            for(int i=0;i<n;i++) {
                sum += p[i][0];
            }
            cnt = sum / s;
            if(sum % s > 0) {
                cnt++;
            }
            same = new boolean[n];
            for(int i=0;i<n;i++) {
                same[i] = p[i][1] == p[i][2];
            }
            long[] wa = new long[n];
            long[] wb = new long[n];
            for(int i=0;i<n;i++) {
                wa[i] = cm(i,1);
                wb[i] = cm(i,2);
            }
            NavigableSet<Integer> byA = new TreeSet<>((x,y) -> Long.compare(wa[y], wa[x]));
            NavigableSet<Integer> byB = new TreeSet<>((x,y) -> Long.compare(wb[y], wb[x]));
            for(int i=0;i<n;i++) {
                byA.add(i);
                byB.add(i);
            }
            sortedA = new int[n];
            sortedB = new int[n];
            Iterator<Integer> itA = byA.iterator();
            Iterator<Integer> itB = byB.iterator();
            for(int i=0;i<n;i++) {
                sortedA[i] = itA.next();
                sortedB[i] = itB.next();
            }
        }

        private long solve() {
            long a0 = happiness(0);
            long an = happiness(cnt);
            long ma = max(a0,an);
            long left = 0;
            long right = cnt;
            while(right - left >= 3) {
                long step = (right - left) / 3;
                long mid1 = left + step;
                long mid2 = mid1 + step;
                long amid1 = happiness(mid1);
                long amid2 = happiness(mid2);
                if(amid1 > amid2) {
                    right = mid2;
                } else {
                    left = mid1;
                }
            }
            long i = left;
            while(i <= left+5 && i <= cnt) {
                ma = max(ma, happiness(i));
                i++;
            }
            return ma;
        }

        private long happiness(long aCnt) {
            Long res = ha.get(aCnt);
            if(res == null) {
                res = computeHappiness(aCnt);
                ha.put(aCnt, res);
            }
            return res;
        }

        private long computeHappiness(long aCnt) {
            int itA = 0;
            int itB = 0;
            long allA = aCnt*s;
            long allB = (cnt-aCnt)*s;
            long[] eaten = new long[n];
            int ua = sortedA[itA++];
            int ub = sortedB[itB++];
            long ha = 0;
            while(ua >= 0 || ub >= 0) {
                if(ua >= 0 && ((eaten[ua] >= p[ua][0]) || same[ua])) {
                    if(same[ua] && eaten[ua] < p[ua][0]) {
                        ha += p[ua][1] * p[ua][0];
                        eaten[ua] = p[ua][0];
                    }
                    if(itA < n) {
                        ua = sortedA[itA++];
                    } else {
                        ua = -1;
                    }
                } else if(ub >= 0 && ((eaten[ub] >= p[ub][0]) || same[ub])) {
                    if(same[ub] && eaten[ub] < p[ub][0]) {
                        ha += p[ub][2] * p[ub][0];
                        eaten[ub] = p[ub][0];
                    }
                    if(itB < n) {
                        ub = sortedB[itB++];
                    } else {
                        ub = -1;
                    }
                } else {
                    if(ua == -1 || allA == 0) {
                        long reqB = p[ub][0] - eaten[ub];
                        long haB = p[ub][2] * reqB;
                        eaten[ub] += reqB;
                        ha += haB;
                        allB -= reqB;
                    } else if(ub == -1 || allB == 0) {
                        long reqA = p[ua][0] - eaten[ua];
                        long haA = p[ua][1] * reqA;
                        eaten[ua] += reqA;
                        ha += haA;
                        allA -= reqA;
                    } else {
                        long reqA = p[ua][0] - eaten[ua];
                        long reqB = p[ub][0] - eaten[ub];
                        if(p[ua][1] > p[ub][2]) {
                            long miA = min(reqA, allA);
                            long haA = p[ua][1] * miA;
                            eaten[ua] += miA;
                            ha += haA;
                            allA -= miA;
                        } else {
                            long miB = min(reqB, allB);
                            long haB = p[ub][2] * miB;
                            eaten[ub] += miB;
                            ha += haB;
                            allB -= miB;
                        }
                    }
                }
            }
            return ha;
        }
    }

    private long[] longarr() throws IOException {
        return longarr(rd.readLine());
    }

    private long[] longarr(String s) {
        String[] q = split(s);
        int n = q.length;
        long[] a = new long[n];
        for(int i=0;i<n;i++) {
            a[i] = Long.parseLong(q[i]);
        }
        return a;
    }

    private int[] intarr() throws IOException {
        return intarr(rd.readLine());
    }

    private int[] intarr(String s) {
        String[] q = split(s);
        int n = q.length;
        int[] a = new int[n];
        for(int i=0;i<n;i++) {
            a[i] = Integer.parseInt(q[i]);
        }
        return a;
    }

    private String[] split(String s) {
        if(s == null) {
            return new String[0];
        }
        int n = s.length();
        int start = -1;
        int end = 0;
        int sp = 0;
        boolean lastWhitespace = true;
        for(int i=0;i<n;i++) {
            char c = s.charAt(i);
            if(isWhitespace(c)) {
                lastWhitespace = true;
            } else {
                if(lastWhitespace) {
                    sp++;
                }
                if(start == -1) {
                    start = i;
                }
                end = i;
                lastWhitespace = false;
            }
        }
        if(start == -1) {
            return new String[0];
        }
        String[] res = new String[sp];
        int last = start;
        int x = 0;
        lastWhitespace = true;
        for(int i=start;i<=end;i++) {
            char c = s.charAt(i);
            boolean w = isWhitespace(c);
            if(w && !lastWhitespace) {
                res[x++] = s.substring(last,i);
            } else if(!w && lastWhitespace) {
                last = i;
            }
            lastWhitespace = w;
        }
        res[x] = s.substring(last,end+1);
        return res;
    }

    private boolean isWhitespace(char c) {
        return c==' ' || c=='\t';
    }

    private static void out(Object x) {
        System.out.println(x);
    }

    public static void main(String[] args) throws IOException {
        new ProblemB12();
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.NavigableSet;
import java.util.TreeSet;

import static java.lang.Math.*;

public class ProblemB6 {
    BufferedReader rd;

    ProblemB6() throws IOException {
        rd = new BufferedReader(new InputStreamReader(System.in));
        compute();
    }

    private void compute() throws IOException {
        int[] u = intarr();
        int n = u[0];
        long s = u[1];

        long[][] p = new long[n][];
        for(int i=0;i<n;i++) {
            p[i] = longarr();
        }
        out(solve(s,p));
    }

    private long solve(long s, long[][] p) {
        Map<Pair, Long> m = new HashMap<>();
        for(long[] u: p) {
            Pair pair = new Pair(u[1], u[2]);
            Long cur = m.get(pair);
            if(cur == null) {
                m.put(pair, u[0]);
            } else {
                m.put(pair, cur + u[0]);
            }
        }
        long[][] p2 = new long[m.size()][3];
        int i = 0;
        for(Map.Entry<Pair, Long> e: m.entrySet()) {
            p2[i][0] = e.getValue();
            p2[i][1] = e.getKey().x;
            p2[i][2] = e.getKey().y;
            i++;
        }
        return new Solver(s, p2).solve();
    }

    class Pair {
        long x,y;

        public Pair(long x, long y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            Pair pair = (Pair) o;

            if (x != pair.x) return false;
            return y == pair.y;
        }

        @Override
        public int hashCode() {
            int result = (int) (x ^ (x >>> 32));
            result = 31 * result + (int) (y ^ (y >>> 32));
            return result;
        }
    }

    private class Solver {
        final long s;
        final int n;
        Map<Long, Long> ha = new HashMap<>();
        long[][] p;
        long sum, cnt;
        NavigableSet<Integer> byA = new TreeSet<>((x,y) -> Long.compare(cm(y,1), cm(x,1)));
        NavigableSet<Integer> byB = new TreeSet<>((x,y) -> Long.compare(cm(y,2), cm(x,2)));
        boolean[] same;

        private long cm(int x, int k) {
            return ((abs(p[x][k]-p[x][3-k]))<<40) + ((p[x][k] << 20) + 1000000 - p[x][3-k]);
        }

        private Solver(long s, long[][] p) {
            this.s = s;
            n = p.length;
            this.p = p;
            for(int i=0;i<n;i++) {
                sum += p[i][0];
            }
            cnt = sum / s;
            if(sum % s > 0) {
                cnt++;
            }
            for(int i=0;i<n;i++) {
                byA.add(i);
                byB.add(i);
            }
            same = new boolean[n];
            for(int i=0;i<n;i++) {
                same[i] = p[i][1] == p[i][2];
            }
        }

        private long solve() {
            long a0 = happiness(0);
            long an = happiness(cnt);
            long ma = max(a0,an);
            long left = 0;
            long right = cnt;
            while(right - left >= 3) {
                long step = (right - left) / 3;
                long mid1 = left + step;
                long mid2 = mid1 + step;
                long amid1 = happiness(mid1);
                long amid2 = happiness(mid2);
                if(amid1 > amid2) {
                    right = mid2;
                } else {
                    left = mid1;
                }
            }
            long i = left;
            while(i <= left+5 && i <= cnt) {
                ma = max(ma, happiness(i));
                i++;
            }
            return ma;
        }

        private long happiness(long aCnt) {
            Long res = ha.get(aCnt);
            if(res == null) {
                res = computeHappiness(aCnt);
                ha.put(aCnt, res);
            }
            return res;
        }

        private long computeHappiness(long aCnt) {
            Iterator<Integer> itA = byA.iterator();
            Iterator<Integer> itB = byB.iterator();
            long allA = aCnt*s;
            long allB = (cnt-aCnt)*s;
            int[] eaten = new int[n];
            int ua = itA.next();
            int ub = itB.next();
            long ha = 0;
            while(ua >= 0 || ub >= 0) {
                if(ua >= 0 && ((eaten[ua] >= p[ua][0]) || same[ua])) {
                    if(same[ua] && eaten[ua] < p[ua][0]) {
                        ha += p[ua][1] * p[ua][0];
                        eaten[ua] = (int)p[ua][0];
                    }
                    if(itA.hasNext()) {
                        ua = itA.next();
                    } else {
                        ua = -1;
                    }
                } else if(ub >= 0 && ((eaten[ub] >= p[ub][0]) || same[ub])) {
                    if(same[ub] && eaten[ub] < p[ub][0]) {
                        ha += p[ub][2] * p[ub][0];
                        eaten[ub] = (int)p[ub][0];
                    }
                    if(itB.hasNext()) {
                        ub = itB.next();
                    } else {
                        ub = -1;
                    }
                } else {
                    if(ua == -1 || allA == 0) {
                        long reqB = p[ub][0] - eaten[ub];
                        long haB = p[ub][2] * reqB;
                        eaten[ub] += reqB;
                        ha += haB;
                        allB -= reqB;
                    } else if(ub == -1 || allB == 0) {
                        long reqA = p[ua][0] - eaten[ua];
                        long haA = p[ua][1] * reqA;
                        eaten[ua] += reqA;
                        ha += haA;
                        allA -= reqA;
                    } else {
                        long reqA = p[ua][0] - eaten[ua];
                        long reqB = p[ub][0] - eaten[ub];
                        long mi = min(min(reqA, allA), min(reqB, allB));
                        long haA = p[ua][1] * mi;
                        long haB = p[ub][2] * mi;
                        if (haA > haB) {
                            eaten[ua] += mi;
                            ha += haA;
                            allA -= mi;
                        } else {
                            eaten[ub] += mi;
                            ha += haB;
                            allB -= mi;
                        }
                    }
                }
            }
            return ha;
        }
    }

    private long[] longarr() throws IOException {
        return longarr(rd.readLine());
    }

    private long[] longarr(String s) {
        String[] q = split(s);
        int n = q.length;
        long[] a = new long[n];
        for(int i=0;i<n;i++) {
            a[i] = Long.parseLong(q[i]);
        }
        return a;
    }

    private int[] intarr() throws IOException {
        return intarr(rd.readLine());
    }

    private int[] intarr(String s) {
        String[] q = split(s);
        int n = q.length;
        int[] a = new int[n];
        for(int i=0;i<n;i++) {
            a[i] = Integer.parseInt(q[i]);
        }
        return a;
    }

    private String[] split(String s) {
        if(s == null) {
            return new String[0];
        }
        int n = s.length();
        int start = -1;
        int end = 0;
        int sp = 0;
        boolean lastWhitespace = true;
        for(int i=0;i<n;i++) {
            char c = s.charAt(i);
            if(isWhitespace(c)) {
                lastWhitespace = true;
            } else {
                if(lastWhitespace) {
                    sp++;
                }
                if(start == -1) {
                    start = i;
                }
                end = i;
                lastWhitespace = false;
            }
        }
        if(start == -1) {
            return new String[0];
        }
        String[] res = new String[sp];
        int last = start;
        int x = 0;
        lastWhitespace = true;
        for(int i=start;i<=end;i++) {
            char c = s.charAt(i);
            boolean w = isWhitespace(c);
            if(w && !lastWhitespace) {
                res[x++] = s.substring(last,i);
            } else if(!w && lastWhitespace) {
                last = i;
            }
            lastWhitespace = w;
        }
        res[x] = s.substring(last,end+1);
        return res;
    }

    private boolean isWhitespace(char c) {
        return c==' ' || c=='\t';
    }

    private static void out(Object x) {
        System.out.println(x);
    }

    public static void main(String[] args) throws IOException {
        new ProblemB6();
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.NavigableSet;
import java.util.TreeSet;

import static java.lang.Math.*;

public class ProblemB7 {
    BufferedReader rd;

    ProblemB7() throws IOException {
        rd = new BufferedReader(new InputStreamReader(System.in));
        compute();
    }

    private void compute() throws IOException {
        int[] u = intarr();
        int n = u[0];
        long s = u[1];

        long[][] p = new long[n][];
        for(int i=0;i<n;i++) {
            p[i] = longarr();
        }
        out(solve(s,p));
    }

    private long solve(long s, long[][] p) {
        Map<Pair, Long> m = new HashMap<>();
        for(long[] u: p) {
            Pair pair = new Pair(u[1], u[2]);
            Long cur = m.get(pair);
            if(cur == null) {
                m.put(pair, u[0]);
            } else {
                m.put(pair, cur + u[0]);
            }
        }
        long[][] p2 = new long[m.size()][3];
        int i = 0;
        for(Map.Entry<Pair, Long> e: m.entrySet()) {
            p2[i][0] = e.getValue();
            p2[i][1] = e.getKey().x;
            p2[i][2] = e.getKey().y;
            i++;
        }
        return new Solver(s, p2).solve();
    }

    class Pair {
        long x,y;

        public Pair(long x, long y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            Pair pair = (Pair) o;

            if (x != pair.x) return false;
            return y == pair.y;
        }

        @Override
        public int hashCode() {
            int result = (int) (x ^ (x >>> 32));
            result = 31 * result + (int) (y ^ (y >>> 32));
            return result;
        }
    }

    private class Solver {
        final long s;
        final int n;
        Map<Long, Long> ha = new HashMap<>();
        long[][] p;
        long sum, cnt;
        boolean[] same;
        int[] sortedA;
        int[] sortedB;

        private long cm(int x, int k) {
            return ((abs(p[x][k]-p[x][3-k]))<<40) + ((p[x][k] << 20) + 1000000 - p[x][3-k]);
        }

        private Solver(long s, long[][] p) {
            this.s = s;
            n = p.length;
            this.p = p;
            for(int i=0;i<n;i++) {
                sum += p[i][0];
            }
            cnt = sum / s;
            if(sum % s > 0) {
                cnt++;
            }
            same = new boolean[n];
            for(int i=0;i<n;i++) {
                same[i] = p[i][1] == p[i][2];
            }
            long[] wa = new long[n];
            long[] wb = new long[n];
            for(int i=0;i<n;i++) {
                wa[i] = cm(i,1);
                wb[i] = cm(i,2);
            }
            NavigableSet<Integer> byA = new TreeSet<>((x,y) -> Long.compare(wa[y], wa[x]));
            NavigableSet<Integer> byB = new TreeSet<>((x,y) -> Long.compare(wb[y], wb[x]));
            for(int i=0;i<n;i++) {
                byA.add(i);
                byB.add(i);
            }
            sortedA = new int[n];
            sortedB = new int[n];
            Iterator<Integer> itA = byA.iterator();
            Iterator<Integer> itB = byB.iterator();
            for(int i=0;i<n;i++) {
                sortedA[i] = itA.next();
                sortedB[i] = itB.next();
            }
        }

        private long solve() {
            long a0 = happiness(0);
            long an = happiness(cnt);
            long ma = max(a0,an);
            long left = 0;
            long right = cnt;
            while(right - left >= 3) {
                long step = (right - left) / 3;
                long mid1 = left + step;
                long mid2 = mid1 + step;
                long amid1 = happiness(mid1);
                long amid2 = happiness(mid2);
                if(amid1 > amid2) {
                    right = mid2;
                } else {
                    left = mid1;
                }
            }
            long i = left;
            while(i <= left+5 && i <= cnt) {
                ma = max(ma, happiness(i));
                i++;
            }
            return ma;
        }

        private long happiness(long aCnt) {
            Long res = ha.get(aCnt);
            if(res == null) {
                res = computeHappiness(aCnt);
                ha.put(aCnt, res);
            }
            return res;
        }

        private long computeHappiness(long aCnt) {
            int itA = 0;
            int itB = 0;
            long allA = aCnt*s;
            long allB = (cnt-aCnt)*s;
            int[] eaten = new int[n];
            int ua = sortedA[itA++];
            int ub = sortedB[itB++];
            long ha = 0;
            while(ua >= 0 || ub >= 0) {
                if(ua >= 0 && ((eaten[ua] >= p[ua][0]) || same[ua])) {
                    if(same[ua] && eaten[ua] < p[ua][0]) {
                        ha += p[ua][1] * p[ua][0];
                        eaten[ua] = (int)p[ua][0];
                    }
                    if(itA < n) {
                        ua = sortedA[itA++];
                    } else {
                        ua = -1;
                    }
                } else if(ub >= 0 && ((eaten[ub] >= p[ub][0]) || same[ub])) {
                    if(same[ub] && eaten[ub] < p[ub][0]) {
                        ha += p[ub][2] * p[ub][0];
                        eaten[ub] = (int)p[ub][0];
                    }
                    if(itB < n) {
                        ub = sortedB[itB++];
                    } else {
                        ub = -1;
                    }
                } else {
                    if(ua == -1 || allA == 0) {
                        long reqB = p[ub][0] - eaten[ub];
                        long haB = p[ub][2] * reqB;
                        eaten[ub] += reqB;
                        ha += haB;
                        allB -= reqB;
                    } else if(ub == -1 || allB == 0) {
                        long reqA = p[ua][0] - eaten[ua];
                        long haA = p[ua][1] * reqA;
                        eaten[ua] += reqA;
                        ha += haA;
                        allA -= reqA;
                    } else {
                        long reqA = p[ua][0] - eaten[ua];
                        long reqB = p[ub][0] - eaten[ub];
                        long mi = min(min(reqA, allA), min(reqB, allB));
                        long haA = p[ua][1] * mi;
                        long haB = p[ub][2] * mi;
                        if (haA > haB) {
                            eaten[ua] += mi;
                            ha += haA;
                            allA -= mi;
                        } else {
                            eaten[ub] += mi;
                            ha += haB;
                            allB -= mi;
                        }
                    }
                }
            }
            return ha;
        }
    }

    private long[] longarr() throws IOException {
        return longarr(rd.readLine());
    }

    private long[] longarr(String s) {
        String[] q = split(s);
        int n = q.length;
        long[] a = new long[n];
        for(int i=0;i<n;i++) {
            a[i] = Long.parseLong(q[i]);
        }
        return a;
    }

    private int[] intarr() throws IOException {
        return intarr(rd.readLine());
    }

    private int[] intarr(String s) {
        String[] q = split(s);
        int n = q.length;
        int[] a = new int[n];
        for(int i=0;i<n;i++) {
            a[i] = Integer.parseInt(q[i]);
        }
        return a;
    }

    private String[] split(String s) {
        if(s == null) {
            return new String[0];
        }
        int n = s.length();
        int start = -1;
        int end = 0;
        int sp = 0;
        boolean lastWhitespace = true;
        for(int i=0;i<n;i++) {
            char c = s.charAt(i);
            if(isWhitespace(c)) {
                lastWhitespace = true;
            } else {
                if(lastWhitespace) {
                    sp++;
                }
                if(start == -1) {
                    start = i;
                }
                end = i;
                lastWhitespace = false;
            }
        }
        if(start == -1) {
            return new String[0];
        }
        String[] res = new String[sp];
        int last = start;
        int x = 0;
        lastWhitespace = true;
        for(int i=start;i<=end;i++) {
            char c = s.charAt(i);
            boolean w = isWhitespace(c);
            if(w && !lastWhitespace) {
                res[x++] = s.substring(last,i);
            } else if(!w && lastWhitespace) {
                last = i;
            }
            lastWhitespace = w;
        }
        res[x] = s.substring(last,end+1);
        return res;
    }

    private boolean isWhitespace(char c) {
        return c==' ' || c=='\t';
    }

    private static void out(Object x) {
        System.out.println(x);
    }

    public static void main(String[] args) throws IOException {
        new ProblemB7();
    }
}