
#ifdef _DEBUG
#include "../../../library/src/debug_template.cpp"
#define DMP(...) dump(#__VA_ARGS__, __VA_ARGS__)
#else
#define DMP(...) ((void)0)
#endif

#include <cassert>
#include <cstdio>
#include <cmath>
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <queue>
#include <numeric>
#include <algorithm>
#include <bitset>
#include <functional>

using namespace std;
using lint = long long;
constexpr int MOD = 1000000007, INF = 1010101010;
constexpr lint LINF = 1LL << 60;

struct init {
    init() {
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(10);
    }
} init_;

int main() {

    lint N, C;
    cin >> N >> C;

    struct dta {
        int a, b, c;
        dta(int a = 0, int b = 0, int c = 0) {}
        bool operator<(dta &r) const { return a < r.a; }
        bool operator>(dta &r) const { return b > r.b; }
    };

    vector<dta> abc(N);
    for (int i = 0; i < N; i++) cin >> abc[i].a >> abc[i].b >> abc[i].c;
    sort(abc.begin(), abc.end());
    //abc.emplace_back(INF, 0, 0);

    lint ans = 0, sum = 0;
    lint now = 0;
    priority_queue<dta, vector<dta>, greater<>> pq;
    for (int i = 0; i < N; i++) {

        ans += min(sum, C) * (abc[i].a - now);
        now = abc[i].a;

        pq.emplace(abc[i]);

        sum += abc[i].c;
        while (!pq.empty() && ((i == N - 1) || abc[i + 1].a > pq.top().b)) {
            ans += min(sum, C) * (pq.top().b - now + 1);
            now = pq.top().b + 1;
            sum -= pq.top().c;
            pq.pop();
        }
        DMP(sum);
    }

    cout << ans << '\n';

    return 0;
}

#include<bits/stdc++.h>
using namespace std;

int sx, sy;
int t[50][50], p[50][50];

vector<int> c(2510, 0);

int score = 0, sum = 0;
string ans = "", s = "";

const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
const char mo[4] = {'R', 'D', 'L', 'U'};

void dfs(int x, int y) {
    if(clock() > 1980000) {
        if(sum > score) {
            ans = s;
        }
        return;
    }

    bool f = true;
    if(clock() <= 1000000){
        for(int i = 0; i < 4; ++i) {
            int nx = x+dx[i], ny = y+dy[i];
            if(nx < 0 || nx >= 50 || ny < 0 || ny >= 50) continue;
            if(c[t[nx][ny]]) continue;
            f = false;

            s.push_back(mo[i]);
            c[t[nx][ny]] = 1;
            sum += p[nx][ny];
            dfs(nx, ny);
            s.pop_back();
            c[t[nx][ny]] = 0;
            sum -= p[nx][ny];
        }
    }
    else {
        for(int i = 3; i >= 0; --i) {
            int nx = x+dx[i], ny = y+dy[i];
            if(nx < 0 || nx >= 50 || ny < 0 || ny >= 50) continue;
            if(c[t[nx][ny]]) continue;
            f = false;

            s.push_back(mo[i]);
            c[t[nx][ny]] = 1;
            sum += p[nx][ny];
            dfs(nx, ny);
            s.pop_back();
            c[t[nx][ny]] = 0;
            sum -= p[nx][ny];
        }
    }

    if(f) {
        if(sum > score) {
            ans = s;
            score = sum;
        }
    }
}

int main() {
    cin >> sx >> sy;
    for(int i = 0; i < 50; ++i) {
        for(int j = 0; j < 50; ++j) scanf("%d", &t[i][j]);
    }
    for(int i = 0; i < 50; ++i) {
        for(int j = 0; j < 50; ++j) scanf("%d", &p[i][j]);
    }

    c[t[sx][sy]] = 1;
    dfs(sx, sy);

    cout << ans << endl;
}