#include <iostream>
#include <fstream>
#include <set>
#include <map>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <functional>
#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
using namespace std;
#define ll long long int
#define rep(i,n) for( int i = 0; i < n; i++ )
#define rrep(i,n) for( int i = n; i >= 0; i-- )
#define REP(i,s,t) for( int i = s; i <= t; i++ )
#define RREP(i,s,t) for( int i = s; i >= t; i-- )
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000
unsigned int randInt() {
    static unsigned int tx = 113456789, ty=362436069, tz=521288629, tw=88675123;
    unsigned int tt = (tx^(tx<<11));
    tx = ty; ty = tz; tz = tw;
    return ( tw=(tw^(tw>>19))^(tt^(tt>>8)) );
}
double randDouble() {
    return double(randInt())/UINT_MAX;
}
#define timeLimit (1.9)
const int64_t CYCLES_PER_SEC=3000000000;
struct Timer {
	int64_t start;
	Timer() { reset(); }
	void reset() { start = getCycle(); }
	inline double get() { return (double)(getCycle() - start) / CYCLES_PER_SEC; }
	inline int64_t getCycle() {
		uint32_t low, high;
		__asm__ volatile ("rdtsc" : "=a" (low), "=d" (high));
		return ((int64_t)low) | ((int64_t)high << 32);
	}
} timer;
int N, M;
string S[1000];
string K[21];
string prev_K[21];
int L = 0;

int score() {
    int ret = 0;
    rep(k, M) {
        bool flag = false;
        rep(i, N) {
            rep(j, N) {
                bool ok = true;
                rep(l, S[k].length())
                    if(K[i][(j + l) % N] != S[k][l]) ok = false;
                if(ok) flag = true;
                if(flag) break; 
            }
            if(flag) break; 
        }
        rep(i, N) {
            rep(j, N) {
                bool ok = true;
                rep(l, S[k].length())
                    if(K[(i + l) % N][j] != S[k][l]) ok = false;
                if(ok) flag = true;
                if(flag) break; 
            }
            if(flag) break; 
        }
        ret += flag;
    }
    return ret;
}
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    // while(timer.get() < timeLimit) {
    //     cout << timer.get() << endl;
    // }
    rep(i, 20) rep(j, 20) {
        K[i] += '.';
        prev_K[i] += '.';
    }
    cin >> N >> M;
    rep(i, M) {
        cin >> S[i];
        L = max<int>(S[i].length() - 2, L);
    }
    int current_score = 0;
    rep(i, M) {
        int x = randInt() % N;
        int y = randInt() % N;
        int d = randInt() % 4;
        //if(S[i].length() >= L) continue;
        int cnt = 0;
        while(K[x][y] != '.' && (K[x][y] != S[i][0] || K[(x + dx[d] * 1 + N) % N][(y + dy[d] * 1 + N) % N ] != S[i][1])) {
            x = randInt() % N;
            y = randInt() % N;
            cnt++;
            if(cnt >= M * 1000) break;
        }
        if(cnt >= M * 1000) break;
        rep(j, S[i].length()) K[x][(y + j) % N] = S[i][j];
        //if(i % 10 == 0) cerr << i << " " << 1e+8 * score()/double(M) << endl;
    }
    // rep(i, M) {
    //     int x = randInt() % N;
    //     int y = randInt() % N;
    //     if(S[i].length() >= L) continue;
    //     rep(i, N) prev_K[i] = K[i];
    //     rep(j, S[i].length()) K[x][(y + j) % N] = S[i][j];
    //     int next_score = score();
    //     if(current_score < next_score) {
    //         current_score = next_score;
    //     } else {
    //         rep(i, N) K[i] = prev_K[i];
    //     }
    //     cerr << i << " " << current_score << endl;
    //     if(timer.get() > timeLimit) break;
    // }
    rep(i, N) {
        rep(j, N) cout << K[i][j];
        cout << endl;
    }
    cerr << 1e+8 * score()/double(M) << endl;



    return 0;
}

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
using namespace std;

typedef long long LL;

#define REP(i, n) for(int i = 0; i < (int)(n); i++)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define ALL(x) (x).begin(),(x).end()
const int IINF = 1e9;
const LL LINF = 1e18;
const LL MOD = 1e9+7;

int main() {
    LL N, C;
    cin >> N >> C;
    vector<LL> a(N), b(N), c(N);

    set<LL> zipped;


    LL M = -1;
    REP(i, N) {
        cin >> a[i] >> b[i] >> c[i];
        b[i]++;
        zipped.insert(a[i]);
        zipped.insert(b[i]);
    }

    map<LL, int> dic;
    map<int, LL> inv;
    int n = 0;
    for(auto i : zipped) {
        dic[i] = n;
        inv[n] = i;
        n++;
    }

    vector<LL> cost(dic.size() + 1, 0);
    REP(i, N) {
        cost[dic[a[i]]] += c[i];
        cost[dic[b[i]]] -= c[i];
    }

    FOR(i, 1, dic.size() + 1) {
        cost[i] += cost[i - 1];
    }
    REP(i, dic.size() + 1) {
        cost[i] = min(cost[i], C);
    }

    LL s = 0;
    REP(i, dic.size()) {
        s += (inv[i + 1] - inv[i])*cost[i];
    }
    cout << s << endl;


    return 0;
}
