#include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <numeric>
#include <list>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <tuple>
#include <deque>
#include <complex>
#include <bitset>
#include <functional>
#include <cassert>
//#include <atcoder/all>

using namespace std;
//using namespace atcoder;

using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using ld = long double;
using vld = vector<ld>;
using vb = vector<bool>;

#define rep(i, n) for (ll i = 0; i < (n); i++)
#ifdef LOCAL
#define dbg(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl
#else
#define dbg(x) true
#endif

template <class T> void chmin(T& a, T b) { a = min(a, b);}
template <class T> void chmax(T& a, T b) { a = max(a, b);}
template <class T> ostream& operator<<(ostream& s, const vector<T>& a) { for(auto i : a) s << i << ' '; return s; }

constexpr ll INFL = 1LL << 60;
constexpr ld EPS = 1e-12;
ld PI = acos(-1.0);

void solve() {
    ll n, cp;
    cin >> n >> cp;
    vpll p;
    rep(i, n) {
        ll a, b, c;
        cin >> a >> b >> c;
        p.emplace_back(a, c);
        p.emplace_back(b+1, -c);
    }
    sort(p.begin(), p.end());
    vpll p2;
    for(auto i : p) {
//        cout << i.first << ' ' << i.second << endl;
        ll sz = p2.size();
//        rep(j, sz) cout << p2[j].first << ':' << p2[j].second << endl;
        if(sz > 0 && p[sz-1].first == i.first) {
//            cout << p[sz-1].second << endl;
            p2[sz-1].second += i.second;
//            cout << "a" << endl;
//            cout << p[sz-1].second << endl;
        }
        else {
            p2.push_back(i);
        }
    }
//    cout << "end" << endl;
    ll last = 0;
    ll now = 0;
    ll ans = 0;
    for(auto i : p2) {
//        cout << i.first << ' ' << i.second << endl;
        ll span = i.first - last;
        ans += min(now, cp) * span;
        last = i.first;
        now += i.second;
    }
    cout << ans << endl;
    return;
}

int main() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    solve();
}
#pragma GCC optimize("O3")
#include<bits/stdc++.h> 
using namespace std;
using ll=long long;
using P=pair<ll,ll>;
template<class T> using V=vector<T>; 
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
const ll inf=(1e18);
//const ll mod=998244353;
const ll mod=1000000007;
const vector<int> dy={-1,0,1,0},dx={0,-1,0,1};
ll GCD(ll a,ll b) {return b ? GCD(b,a%b):a;}
ll LCM(ll c,ll d){return c/GCD(c,d)*d;}
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(20);}} __init;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
template<class T>void debag(const vector<T> &a){cerr<<"debag :";for(auto v:a)cerr<<v<<" ";cerr<<"\n";}
template<class T>void print(const vector<T> &a){for(auto v:a)cout<<v<<" ";cout<<"\n";}
int main(){
    int n;
    cin>>n;
    string s;
    cin>>s;
    n++;
    V<ll> a(n);
    for(int i=0;i<n;i++)cin>>a[i];
    ll mi=inf;
    for(int i=1;i<n;i++){
         chmin(mi,abs(a[i-1]-a[i]));
    }
    V<ll> res(n);
    for(int i=0;i<n;i++){
          res[i]=a[i]/mi;
          a[i]%=mi;
    }
V<V<ll>> ans(mi,res);
int num=0;
   while(*max_element(all(a))!=0){
          for(int i=0;i<n;i++){
              if(a[i]!=0){
                ans[num][i]++;
                a[i]--;
              }
          }
          num++;
   }
   cout<<mi<<"\n";
   for(int i=0;i<mi;i++)print(ans[i]);
}