#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 2010,INF = 0x3f3f3f3f;

string s;
int n,m,tmp,p,ans,sum,minn = INF;
int a[N][N];

int main()
{
	cin >> n >> m;
	for(int i = 0;i < n;i++)
	{
		for(int j = 0;j < m;j++)
		{
			cin >> a[i][j];
			minn = min(minn,a[i][j]);
		}
	}
	for(int i = 0;i < n;i++)
	{
		for(int j = 0;j < m;j++)
		{
			sum += a[i][j]-minn;
		}
	}
	cout << sum << endl;
	
	return 0;
}

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
#pragma GCC optimize("no-stack-protector")

#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ld long double
#define ll long long
#define REP(i, a, b) for (ll i = a; i < b; i++)
#define REPI(i, a, b) for (ll i = b - 1; i >= a; i--)
#define i_os ios::sync_with_stdio(0);  cin.tie(0);  cout.tie(0);
#define INF (ll)1e18 + 100
#define endl "\n"
#define p0(a) cout << a << " "
#define p1(a) cout << a << endl
#define p2(a, b) cout << a << " " << b << endl
#define p3(a, b, c) cout << a << " " << b << " " << c << endl
#define p4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl
// SOME BITMASK KNOWLEDGE
// 1)x & (x - 1):sets the last one bit of x to zero
// power of two exactly when x & (x − 1) = 0.
// 2)x & -x:sets all the one bits to zero, except last one bit
// 3)x | (x - 1):inverts all the bits after the last one bit
#define o_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define o_setll tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
typedef tree<pair<ll, ll>,null_type,less<pair<ll, ll>>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> indexed_set1;
typedef tree<string,null_type,less<string>,rb_tree_tag,tree_order_statistics_node_update> indexed_set2;
//1. order_of_key(k) : number of elements strictly lesser than k
//2. find_by_order(k) : k-th element in the set
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);

ll power(ll a,ll n){
	if(n == 0) return 1;
	ll s = power(a,n/2);
	s = (s * s);
	if(n % 2) s = (s * a);
	return s;
}

struct Service {
	ll l,c,f;
};

bool compare(Service a,Service b){
	if(a.l == b.l){
		return a.f > b.f;
	}
	return a.l < b.l;
}
int main()
{
	i_os;
	ll n,C;
	cin>>n>>C;
	vector<Service> v;
	REP(i,0,n){
		ll x,y,z;
		cin>>x>>y>>z;
		Service s1,s2;
		s1.l = x; s1.c = z; s1.f = 0;
		s2.l = y+1; s2.c = z; s2.f = 1;
		v.push_back(s1);
		v.push_back(s2);
	}
	sort(v.begin(),v.end(),compare);
	ll sum = 0;
	ll ans = 0;
	ll in = 0;
	ll st = 1;
	for(Service s:v){
		// cout<<s.l<<" "<<s.f<<" "<<s.c<<endl;
		if(s.f){
			ans += min(C,sum)*(s.l-1-st+1);
			st = s.l;
			sum -= s.c;
			in--;
		}
		else {
			if(in != 0){
				ans += min(C,sum)*(s.l-1-st+1);
			}
			st = s.l;
			sum += s.c;
			in++;
		}
	}
	cout<<ans<<endl;
	return 0;
}