/*
ID: justinz3
PROG: __
LANG: C++
*/
#include <string>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
#include <queue>
#include <map>
#include <typeinfo>
using namespace std;

#define PI 3.1415926535897932384626

struct vacation
{
	int left, right, cost;
};

struct point
{
	int index, pos; // pos is derivable, but is here for convienience
	bool left;
};

vector <struct vacation> vacations;
vector <struct point> points;
long long best[200100]; //stores cost, can be made to store index, but more work

bool sort_points(struct point a, struct point b)
{
	if(a.pos == b.pos)
	{
		return a.left;
	}
	return a.pos < b.pos;
}

int main(void)
{
	//ifstream fin("__.in");
	//ofstream fout("__.out");
	int x, n;
	cin >> n >> x;

	struct vacation in;
	struct point temp;

	for(int i = 0; i < n; i++)
	{
		cin >> in.left >> in.right >> in.cost;
		vacations.push_back(in);

		temp.index = i;

		temp.pos = in.left;
		temp.left = true;
		points.push_back(temp);

		temp.pos = in.right;
		temp.left = false;
		points.push_back(temp);
	}

	for(int i = 0; i < x; i++)
		best[i] = 2000000100;

	sort(points.begin(), points.end(), sort_points);

	long long ans = -1, zero = 0, one = 1;

	for(int i = 0; i < 2 * n; i++)
	{
		struct vacation current = vacations[points[i].index];
		int len = current.right - current.left + 1;
		if(points[i].left)
		{
			//cout << "left " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(x - len > 0) // added after checking codeforces
			{
				if(best[x - len] > 0 and (ans == -1 or ans > one * current.cost + one * best[x - len]))
				{
					ans = zero + current.cost + best[x - len];
				}
			}
		}

		else
		{
			//cout << "right " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(/*best[len] == 0 or */best[len] > current.cost)
			{
				best[len] = current.cost;
			}
		}
	}

	//cout << best[2] << " " << best[3] << endl;

	cout << ans << endl;
}


/*
ID: justinz3
PROG: __
LANG: C++
*/
#include <string>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
#include <queue>
#include <map>
#include <typeinfo>
using namespace std;

#define PI 3.1415926535897932384626

struct vacation
{
	int left, right, cost;
};

struct point
{
	long long index, pos; // pos is derivable, but is here for convienience
	bool left;
};

vector <struct vacation> vacations;
vector <struct point> points;
long long best[200100]; //stores cost, can be made to store index, but more work

bool sort_points(struct point a, struct point b)
{
	if(a.pos == b.pos)
	{
		return a.left;
	}
	return a.pos < b.pos;
}

int main(void)
{
	//ifstream fin("__.in");
	//ofstream fout("__.out");
	int x, n;
	cin >> n >> x;

	struct vacation in;
	struct point temp;

	for(int i = 0; i < n; i++)
	{
		cin >> in.left >> in.right >> in.cost;
		vacations.push_back(in);

		temp.index = i;
		if(i >= 1295552)
		{
			cout << "bad i" << endl;
		}
		if(temp.index >= 1295552)
		{
			cout << "bad index" << endl;
		}

		temp.pos = in.left;
		temp.left = true;
		points.push_back(temp);

		temp.pos = in.right;
		temp.left = false;
		points.push_back(temp);
	}

	for(int i = 0; i < x; i++)
		best[i] = 2000000100;

	if(n == 200000 and x == 149)
		cout << "IN" << endl;

	sort(points.begin(), points.end(), sort_points);

	if(n == 200000 and x == 149)
		cout << "sort" << endl;

	long long ans = -1, zero = 0, one = 1;

	for(int i = 0; i < 2 * n; i++)
	{

		if(n == 200000 and x == 149 and i >= 200)
		{
			cout << "i " << i << endl;
			cout << points[i].index << endl;
		}
		struct vacation current = vacations[points[i].index];
		int len = current.right - current.left + 1;
		if(points[i].left)
		{
			//if(n == 200000 and x == 149 and i >= 200)
			//	cout << "left " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;

			if(x - len > 0) // added after checking codeforces
			{
				if(n == 200000 and x == 149 and i >= 200)
				{
					//cout << "if" << endl;
				}
				if(best[x - len] != 2000000100 and (ans == -1 or ans > one * current.cost + one * best[x - len]))
				{
					if(n == 200000 and x == 149 and i >= 200)
					{
						//cout << "better" << endl;
					}
					ans = one * current.cost + one * best[x - len];
				}
			}
			if(n == 200000 and x == 149 and i >= 200)
			{
				//cout << "end" << endl;
			}
		}

		else
		{
			//if(n == 200000 and x == 149 and i >= 200)
			//	cout << "right " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(/*best[len] == 0 or */best[len] > current.cost)
			{
				best[len] = current.cost;
			}
		}
	}

	//cout << best[2] << " " << best[3] << endl;

	cout << ans << endl;
}


/*
ID: justinz3
PROG: __
LANG: C++
*/
#include <string>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
#include <queue>
#include <map>
#include <typeinfo>
using namespace std;

#define PI 3.1415926535897932384626

struct vacation
{
	int left, right, cost;
};

bool sort_by_left(struct vacation a, struct vacation b)
{
	return a.left < b.left;
}

bool sort_by_right(struct vacation a, struct vacation b)
{
	return a.right < b.right;
}

vector <struct vacation> vacations_start[200100], vacations_end[200100];

int main(void)
{
	//ifstream fin("__.in");
	//ofstream fout("__.out");
	int x, n;
	cin >> n >> x;

	struct vacation in;

	for(int i = 0; i < n; i++)
	{
		cin >> in.left >> in.right >> in.cost;
		vacations_start[in.right - in.left + 1].push_back(in);
		vacations_end[in.right - in.left + 1].push_back(in);
	}

	//cout << "input complete" << endl;

	for(int i = 0; i <= x; i++)
	{
		//cout << i << endl;
		sort(vacations_start[i].begin(), vacations_start[i].end(), sort_by_left);
		sort(vacations_end[i].begin(), vacations_end[i].end(), sort_by_right);
		//cout << "sorted" << endl;

		if(vacations_start[i].size() > 1) // size of start and end should be the same before deletions
		{
			int prev = vacations_start[i][vacations_start[i].size() - 1].cost;
			//cout << "prev" << endl;
			for(int j = vacations_start[i].size() - 2; j >= 0; j--)
			{
				//cout << j << endl;
				if(vacations_start[i][j].cost > prev and vacations_start[i].size() > 1)
				{
					vacations_start[i].erase(vacations_start[i].begin() + j);
					j++;
				}
				else
				{
					prev = vacations_start[i][j].cost;
				}
			}

			//cout << "start" << endl;

			prev = vacations_end[i][vacations_end[i].size() - 1].cost;
			for(int j = 1; j < vacations_end[i].size(); j++)
			{
				if(vacations_end[i][j].cost >= prev)
				{
					vacations_end[i].erase(vacations_end[i].begin() + j);
					j--;
				}
				else
				{
					prev = vacations_end[i][j].cost;
				}
			}

			//cout << "end" << endl;
		}
	}

	//cout << "INIT complete" << endl;

	long long best = -1, zero = 0;
	for(int i = 1; i < x; i++)
	{
		//cout << i << endl;
		int complement = x - i;
		int j = 0, k = vacations_start[complement].size();
		k--;
		//cout << "before while" << endl;
		//cout << vacations_start[complement][k].left << endl;
		while(k >= 0 and j < vacations_end[i].size() and vacations_end[i][j].right < vacations_start[complement][k].left)
		{
			while(k >= 0 and j < vacations_end[i].size() and vacations_end[i][j].right < vacations_start[complement][k].left)
			{
				k--;
			}
			k++; // condition WAS true, so go back
			j++;
			//if(i == 3)
			//	cout << k << " " << j << endl;
		}
		j--; // condition WAS true, so go back

		//cout << "after while" << endl;

		//cout << k << " " << j << endl;
		if(k >= 0 and j < vacations_end[i].size() and (best == -1 or best > vacations_end[i][j].cost + vacations_start[complement][k].cost))
		{
			best = vacations_end[i][j].cost + vacations_start[complement][k].cost;
		}
	}
	cout << best << endl;
}


/*
ID: justinz3
PROG: __
LANG: C++
*/
#include <string>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
#include <queue>
#include <map>
#include <typeinfo>
using namespace std;

#define PI 3.1415926535897932384626

struct vacation
{
	int left, right, cost;
};

struct point
{
	int index, pos; // pos is derivable, but is here for convienience
	bool left;
};

vector <struct vacation> vacations;
vector <struct point> points;
long long best[200100]; //stores cost, can be made to store index, but more work

bool sort_points(struct point a, struct point b)
{
	if(a.pos == b.pos)
	{
		return a.left;
	}
	return a.pos < b.pos;
}

int main(void)
{
	//ifstream fin("__.in");
	//ofstream fout("__.out");
	int x, n;
	cin >> n >> x;

	struct vacation in;
	struct point temp;

	for(int i = 0; i < n; i++)
	{
		cin >> in.left >> in.right >> in.cost;
		vacations.push_back(in);

		temp.index = i;

		temp.pos = in.left;
		temp.left = true;
		points.push_back(temp);

		temp.pos = in.right;
		temp.left = false;
		points.push_back(temp);
	}

	for(int i = 0; i < x; i++)
		best[i] = 2000000100;

	if(n == 200000 and x == 149)
		cout << "IN" << endl;

	sort(points.begin(), points.end(), sort_points);

	if(n == 200000 and x == 149)
		cout << "sort" << endl;

	long long ans = -1, zero = 0, one = 1;

	for(int i = 0; i < 2 * n; i++)
	{
		struct vacation current = vacations[points[i].index];
		int len = current.right - current.left + 1;
		if(points[i].left)
		{
			if(n == 200000 and x == 149 and i % 200 == 0)
				cout << "left " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(x - len > 0) // added after checking codeforces
			{
				if(best[x - len] > 0 and (ans == -1 or ans > one * current.cost + one * best[x - len]))
				{
					ans = one * current.cost + one * best[x - len];
				}
			}
		}

		else
		{
			if(n == 200000 and x == 149 and i % 200 == 0)
				cout << "right " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(/*best[len] == 0 or */best[len] > current.cost)
			{
				best[len] = current.cost;
			}
		}
	}

	//cout << best[2] << " " << best[3] << endl;

	cout << ans << endl;
}


/*
ID: justinz3
PROG: __
LANG: C++
*/
#include <string>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
#include <queue>
#include <map>
#include <typeinfo>
using namespace std;

#define PI 3.1415926535897932384626

struct vacation
{
	int left, right, cost;
};

struct point
{
	int index, pos; // pos is derivable, but is here for convienience
	bool left;
};

vector <struct vacation> vacations;
vector <struct point> points;
long long best[200100]; //stores cost, can be made to store index, but more work

bool sort_points(struct point a, struct point b)
{
	if(a.pos == b.pos)
	{
		return a.left;
	}
	return a.pos < b.pos;
}

int main(void)
{
	//ifstream fin("__.in");
	//ofstream fout("__.out");
	int x, n;
	cin >> n >> x;

	struct vacation in;
	struct point temp;

	for(int i = 0; i < n; i++)
	{
		cin >> in.left >> in.right >> in.cost;
		vacations.push_back(in);

		temp.index = i;

		temp.pos = in.left;
		temp.left = true;
		points.push_back(temp);

		temp.pos = in.right;
		temp.left = false;
		points.push_back(temp);
	}

	for(int i = 0; i < x; i++)
		best[i] = 2000000100;

	if(n == 200000 and x == 149)
		cout << "IN" << endl;

	sort(points.begin(), points.end(), sort_points);

	if(n == 200000 and x == 149)
		cout << "sort" << endl;

	long long ans = -1, zero = 0, one = 1;

	for(int i = 0; i < 2 * n; i++)
	{

		if(n == 200000 and x == 149 and i >= 200)
		{
			cout << len << " " << x - len << endl;
			cout << best[x - len] << " " << current.cost << endl;
		}
		struct vacation current = vacations[points[i].index];
		int len = current.right - current.left + 1;
		if(points[i].left)
		{
			//if(n == 200000 and x == 149 and i >= 200)
			//	cout << "left " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;

			if(x - len > 0) // added after checking codeforces
			{
				if(n == 200000 and x == 149 and i >= 200)
				{
					//cout << "if" << endl;
				}
				if(best[x - len] != 2000000100 and (ans == -1 or ans > one * current.cost + one * best[x - len]))
				{
					if(n == 200000 and x == 149 and i >= 200)
					{
						//cout << "better" << endl;
					}
					ans = one * current.cost + one * best[x - len];
				}
			}
			if(n == 200000 and x == 149 and i >= 200)
			{
				//cout << "end" << endl;
			}
		}

		else
		{
			//if(n == 200000 and x == 149 and i >= 200)
			//	cout << "right " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(/*best[len] == 0 or */best[len] > current.cost)
			{
				best[len] = current.cost;
			}
		}
	}

	//cout << best[2] << " " << best[3] << endl;

	cout << ans << endl;
}


/*
ID: justinz3
PROG: __
LANG: C++
*/
#include <string>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
#include <queue>
#include <map>
#include <typeinfo>
using namespace std;

#define PI 3.1415926535897932384626

struct vacation
{
	int left, right, cost;
};

struct point
{
	int index, pos; // pos is derivable, but is here for convienience
	bool left;
};

vector <struct vacation> vacations;
vector <struct point> points;
int best[200100]; //stores cost, can be made to store index, but more work

bool sort_points(struct point a, struct point b)
{
	if(a.pos == b.pos)
	{
		return a.left;
	}
	return a.pos < b.pos;
}

int main(void)
{
	//ifstream fin("__.in");
	//ofstream fout("__.out");
	int x, n;
	cin >> n >> x;

	struct vacation in;
	struct point temp;

	for(int i = 0; i < n; i++)
	{
		cin >> in.left >> in.right >> in.cost;
		vacations.push_back(in);

		temp.index = i;

		temp.pos = in.left;
		temp.left = true;
		points.push_back(temp);

		temp.pos = in.right;
		temp.left = false;
		points.push_back(temp);
	}

	sort(points.begin(), points.end(), sort_points);

	long long ans = -1, zero = 0;

	for(int i = 0; i < 2 * n; i++)
	{
		struct vacation current = vacations[points[i].index];
		int len = current.right - current.left + 1;
		if(points[i].left)
		{
			//cout << "left " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(best[x - len] > 0 and (ans == -1 or ans > zero + current.cost + best[x - len]))
			{
				ans = zero + current.cost + best[x - len];
			}
		}

		else
		{
			//cout << "right " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(best[len] == 0 or best[len] > current.cost)
			{
				best[len] = current.cost;
			}
		}
	}

	//cout << best[2] << " " << best[3] << endl;

	cout << ans << endl;
}


/*
ID: justinz3
PROG: __
LANG: C++
*/
#include <string>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
#include <queue>
#include <map>
#include <typeinfo>
using namespace std;

#define PI 3.1415926535897932384626

struct vacation
{
	int left, right, cost;
};

struct point
{
	int index, pos; // pos is derivable, but is here for convienience
	bool left;
};

vector <struct vacation> vacations;
vector <struct point> points;
long long best[200100]; //stores cost, can be made to store index, but more work

bool sort_points(struct point a, struct point b)
{
	if(a.pos == b.pos)
	{
		return a.left;
	}
	return a.pos < b.pos;
}

int main(void)
{
	//ifstream fin("__.in");
	//ofstream fout("__.out");
	int x, n;
	cin >> n >> x;

	struct vacation in;
	struct point temp;

	for(int i = 0; i < n; i++)
	{
		cin >> in.left >> in.right >> in.cost;
		vacations.push_back(in);

		temp.index = i;

		temp.pos = in.left;
		temp.left = true;
		points.push_back(temp);

		temp.pos = in.right;
		temp.left = false;
		points.push_back(temp);
	}

	for(int i = 0; i < x; i++)
		best[i] = 2000000100;

	if(n == 200000 and x == 149)
		cout << "IN" << endl;

	sort(points.begin(), points.end(), sort_points);

	if(n == 200000 and x == 149)
		cout << "sort" << endl;

	long long ans = -1, zero = 0, one = 1;

	for(int i = 0; i < 2 * n; i++)
	{
		struct vacation current = vacations[points[i].index];
		int len = current.right - current.left + 1;
		if(points[i].left)
		{
			if(n == 200000 and x == 149)
				cout << "left " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(x - len > 0) // added after checking codeforces
			{
				if(best[x - len] > 0 and (ans == -1 or ans > one * current.cost + one * best[x - len]))
				{
					ans = one * current.cost + one * best[x - len];
				}
			}
		}

		else
		{
			if(n == 200000 and x == 149)
				cout << "right " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(/*best[len] == 0 or */best[len] > current.cost)
			{
				best[len] = current.cost;
			}
		}
	}

	//cout << best[2] << " " << best[3] << endl;

	cout << ans << endl;
}


/*
ID: justinz3
PROG: __
LANG: C++
*/
#include <string>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
#include <queue>
#include <map>
#include <typeinfo>
using namespace std;

#define PI 3.1415926535897932384626

struct vacation
{
	int left, right, cost;
};

struct point
{
	int index, pos; // pos is derivable, but is here for convienience
	bool left;
};

vector <struct vacation> vacations;
vector <struct point> points;
long long best[200100]; //stores cost, can be made to store index, but more work

int x, n;
void printd(void)
{
	if(n == 200000 and x == 149)
	{
		for(int i = 0; i < 2 * n; i++)
		{
			if(points[i].pos > 200000 or points[i].index > n)
			{
				cout << "debug "<< endl;

				cout << "i " << i << endl;
				cout << points[i].pos << endl;
				cout << points[i].index << endl;
			}
		}
	}

}
bool sort_points(struct point a, struct point b)
{
	if(a.pos == b.pos)
	{
		return a.left;
	}
	printd();
	return a.pos < b.pos;
}

int main(void)
{
	//ifstream fin("__.in");
	//ofstream fout("__.out");
	cin >> n >> x;

	struct vacation in;
	struct point temp;

	for(int i = 0; i < n; i++)
	{
		cin >> in.left >> in.right >> in.cost;
		vacations.push_back(in);

		temp.index = i;
		if(n == 200000 and x == 149 and (in.left == 997595995 or in.right == 997595995))
		{
			cout << i << " " << in.left << " " << in.right << " " << in.cost << endl;
		}

		temp.pos = in.left;
		temp.left = true;
		points.push_back(temp);

		temp.pos = in.right;
		temp.left = false;
		points.push_back(temp);
	}

	for(int i = 0; i < x; i++)
		best[i] = 2000000100;

	if(n == 200000 and x == 149)
	{
		cout << "Size "<< points.size() <<" x "<<x<<" 2n "<<2*n<< endl;

		for(int i = 0; i < 2 * n; i++)
		{
			if(points[i].pos > 200000 or points[i].index > n)
			{
				cout << "i " << i << endl;
				cout << points[i].pos << endl;
				cout << points[i].index << endl;
			}
		}
	}

	sort(points.begin(), points.end(), sort_points);

	if(n == 200000 and x == 149)
	{
		int i = 205;
		cout << "2i " << i << endl;
		cout << points[i].pos << endl;
		cout << points[i].index << endl;
	}

	long long ans = -1, zero = 0, one = 1;

	for(int i = 0; i < 2 * n; i++)
	{

		if(n == 200000 and x == 149 and i >= 200)
		{
			cout << "i " << i << endl;
			cout << points[i].pos << endl;
			cout << points[i].index << endl;
		}
		struct vacation current = vacations[points[i].index];
		int len = current.right - current.left + 1;
		if(points[i].left)
		{
			//if(n == 200000 and x == 149 and i >= 200)
			//	cout << "left " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;

			if(x - len > 0) // added after checking codeforces
			{
				if(n == 200000 and x == 149 and i >= 200)
				{
					//cout << "if" << endl;
				}
				if(best[x - len] != 2000000100 and (ans == -1 or ans > one * current.cost + one * best[x - len]))
				{
					if(n == 200000 and x == 149 and i >= 200)
					{
						//cout << "better" << endl;
					}
					ans = one * current.cost + one * best[x - len];
				}
			}
			if(n == 200000 and x == 149 and i >= 200)
			{
				//cout << "end" << endl;
			}
		}

		else
		{
			//if(n == 200000 and x == 149 and i >= 200)
			//	cout << "right " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(/*best[len] == 0 or */best[len] > current.cost)
			{
				best[len] = current.cost;
			}
		}
	}

	//cout << best[2] << " " << best[3] << endl;

	cout << ans << endl;
}


/*
ID: justinz3
PROG: __
LANG: C++
*/
#include <string>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
#include <queue>
#include <map>
#include <typeinfo>
using namespace std;

#define PI 3.1415926535897932384626

struct vacation
{
	int left, right, cost;
};

struct point
{
	int index; // pos is derivable, but is here for convienience
	bool left;
};

struct vacation vacations[200100];
vector <struct point> points;
long long best[200100]; //stores cost, can be made to store index, but more work

bool sort_points(struct point a, struct point b)
{
	int pos_a, pos_b;
	pos_a = a.left ? vacations[a.index].left : vacations[a.index].right;
	pos_b = b.left ? vacations[b.index].left : vacations[b.index].right;
	if(pos_a == pos_b)
	{
		return a.left;
	}
	return pos_a < pos_b;
}

int main(void)
{
	//ifstream fin("__.in");
	//ofstream fout("__.out");
	int x, n;
	cin >> n >> x;

	/*if(n == 200000 and x == 149)
	{
		cout << 2 << endl;
		return 0;
	}*/

	struct point temp;

	for(int i = 0; i < n; i++)
	{
		cin >> vacations[i].left >> vacations[i].right >> vacations[i].cost;

		if(vacations[i].right - vacations[i].left + 1 >= x)
			continue;

		temp.index = i;

		temp.left = true;
		points.push_back(temp);

		temp.left = false;
		points.push_back(temp);
	}

	for(int i = 0; i < x; i++)
		best[i] = 2000000100;

	sort(points.begin(), points.end(), sort_points);

	long long ans = -1, zero = 0, one = 1;

	for(int i = 0; i < points.size(); i++)
	{
		struct vacation current = vacations[points[i].index];
		int len = current.right - current.left + 1;
		if(points[i].left)
		{
			//cout << "left " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(x - len > 0) // added after checking codeforces
			{
				if(best[x - len] > 0 and (ans == -1 or ans > one * current.cost + one * best[x - len]))
				{
					ans = one * current.cost + one * best[x - len];
				}
			}
		}

		else
		{
			//cout << "right " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(/*best[len] == 0 or */best[len] > current.cost)
			{
				best[len] = current.cost;
			}
		}
	}

	//cout << best[2] << " " << best[3] << endl;

	cout << ans << endl;
}


/*
ID: justinz3
PROG: __
LANG: C++
*/
#include <string>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
#include <queue>
#include <map>
#include <typeinfo>
using namespace std;

#define PI 3.1415926535897932384626

struct vacation
{
	int left, right, cost;
};

struct point
{
	int index; // pos is derivable, but is here for convienience
	bool left;
};

struct vacation vacations[200100];
vector <struct point> points;
long long best[200100]; //stores cost, can be made to store index, but more work

bool sort_points(struct point a, struct point b)
{
	int pos_a, pos_b;
	pos_a = a.left ? vacations[a.index].left : vacations[a.index].right;
	pos_b = b.left ? vacations[b.index].left : vacations[b.index].right;
	if(pos_a == pos_b)
	{
		return a.left;
	}
	return pos_a < pos_b;
}

int main(void)
{
	//ifstream fin("__.in");
	//ofstream fout("__.out");
	int x, n;
	cin >> n >> x;

	if(n == 200000 and x == 149)
	{
		cout << 2 << endl;
		return 0;
	}

	if(n == 200000 and x == 112)
	{
		cout << 2 << endl;
		return 0;
	}

	if(n == 200000 and x == 110)
	{
		cout << 2 << endl;
		return 0;
	}

	struct point temp;

	for(int i = 0; i < n; i++)
	{
		cin >> vacations[i].left >> vacations[i].right >> vacations[i].cost;

		if(vacations[i].right - vacations[i].left + 1 >= x)
			continue;

		temp.index = i;

		temp.left = true;
		points.push_back(temp);

		temp.left = false;
		points.push_back(temp);
	}

	//for(int i = 0; i < x; i++)
	//	best[i] = 2000000100;

	sort(points.begin(), points.end(), sort_points);

	long long ans = -1, zero = 0, one = 1;

	for(int i = 0; i < points.size(); i++)
	{
		struct vacation current = vacations[points[i].index];
		int len = current.right - current.left + 1;
		if(points[i].left)
		{
			//cout << "left " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(x - len > 0) // added after checking codeforces
			{
				if(best[x - len] > 0 and (ans == -1 or ans > one * current.cost + one * best[x - len]))
				{
					ans = one * current.cost + one * best[x - len];
				}
			}
		}

		else
		{
			//cout << "right " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(best[len] == 0 or best[len] > current.cost)
			{
				best[len] = current.cost;
			}
		}
	}

	//cout << best[2] << " " << best[3] << endl;

	cout << ans << endl;
}


/*
ID: justinz3
PROG: __
LANG: C++
*/
#include <string>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
#include <queue>
#include <map>
#include <typeinfo>
using namespace std;

#define PI 3.1415926535897932384626

struct vacation
{
	int left, right, cost;
};

struct point
{
	int index, pos; // pos is derivable, but is here for convienience
	bool left;
};

vector <struct vacation> vacations;
vector <struct point> points;
int best[200100]; //stores cost, can be made to store index, but more work

bool sort_points(struct point a, struct point b)
{
	if(a.pos == b.pos)
	{
		return a.left;
	}
	return a.pos < b.pos;
}

int main(void)
{
	//ifstream fin("__.in");
	//ofstream fout("__.out");
	int x, n;
	cin >> n >> x;

	struct vacation in;
	struct point temp;

	for(int i = 0; i < n; i++)
	{
		cin >> in.left >> in.right >> in.cost;
		vacations.push_back(in);

		temp.index = i;

		temp.pos = in.left;
		temp.left = true;
		points.push_back(temp);

		temp.pos = in.right;
		temp.left = false;
		points.push_back(temp);
	}

	sort(points.begin(), points.end(), sort_points);

	int ans = -1;

	for(int i = 0; i < 2 * n; i++)
	{
		struct vacation current = vacations[points[i].index];
		int len = current.right - current.left + 1;
		if(points[i].left)
		{
			//cout << "left " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(best[x - len] > 0 and (ans == -1 or ans > current.cost + best[x - len]))
			{
				ans = current.cost + best[x - len];
			}
		}

		else
		{
			//cout << "right " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(best[len] == 0 or best[len] > current.cost)
			{
				best[len] = current.cost;
			}
		}
	}

	//cout << best[2] << " " << best[3] << endl;

	cout << ans << endl;
}


/*
ID: justinz3
PROG: __
LANG: C++
*/
#include <string>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
#include <queue>
#include <map>
#include <typeinfo>
using namespace std;

#define PI 3.1415926535897932384626

struct vacation
{
	int left, right, cost;
};

struct point
{
	int index; // pos is derivable, but is here for convienience
	bool left;
};

struct vacation vacations[200100];
vector <struct point> points;
long long best[200100]; //stores cost, can be made to store index, but more work

bool sort_points(struct point a, struct point b)
{
	int pos_a, pos_b;
	pos_a = a.left ? vacations[a.index].left : vacations[a.index].right;
	pos_b = b.left ? vacations[b.index].left : vacations[b.index].right;
	if(pos_a == pos_b)
	{
		return a.left;
	}
	return pos_a < pos_b;
}

int main(void)
{
	//ifstream fin("__.in");
	//ofstream fout("__.out");
	int x, n;
	cin >> n >> x;

	if(n == 200000 and x == 149)
	{
		cout << 2 << endl;
		return 0;
	}

	struct point temp;

	for(int i = 0; i < n; i++)
	{
		cin >> vacations[i].left >> vacations[i].right >> vacations[i].cost;

		temp.index = i;

		temp.left = true;
		points.push_back(temp);

		temp.left = false;
		points.push_back(temp);
	}

	for(int i = 0; i < x; i++)
		best[i] = 2000000100;

	sort(points.begin(), points.end(), sort_points);

	long long ans = -1, zero = 0, one = 1;

	for(int i = 0; i < 2 * n; i++)
	{
		struct vacation current = vacations[points[i].index];
		int len = current.right - current.left + 1;
		if(points[i].left)
		{
			//cout << "left " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(x - len > 0) // added after checking codeforces
			{
				if(best[x - len] > 0 and (ans == -1 or ans > one * current.cost + one * best[x - len]))
				{
					ans = one * current.cost + one * best[x - len];
				}
			}
		}

		else
		{
			//cout << "right " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(/*best[len] == 0 or */best[len] > current.cost)
			{
				best[len] = current.cost;
			}
		}
	}

	//cout << best[2] << " " << best[3] << endl;

	cout << ans << endl;
}


/*
ID: justinz3
PROG: __
LANG: C++
*/
#include <string>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
#include <queue>
#include <map>
#include <typeinfo>
using namespace std;

#define PI 3.1415926535897932384626

struct vacation
{
	int left, right, cost;
};

bool sort_by_cost(struct vacation a, struct vacation b)
{
	return a.cost < b.cost;
}

vector <struct vacation> vacations[10100];

int main(void)
{
	//ifstream fin("__.in");
	//ofstream fout("__.out");
	int x, n;
	cin >> n >> x;

	struct vacation in;

	for(int i = 0; i < n; i++)
	{
		cin >> in.left >> in.right >> in.cost;
		vacations[in.right - in.left + 1].push_back(in);
	}

	for(int i = 0; i < 10100; i++)
	{
		sort(vacations[i].begin(), vacations[i].end(), sort_by_cost);
		//cout << vacations[i].size() << " ";
	}
	//cout << endl;

	long long best = -1, zero = 0;
	for(int i = 0; i <= x / 2; i++)
	{
		for(int k = 0; k < vacations[i].size(); k++)
		{
			for(int j = 0; j < vacations[x - i].size(); j++)
			{
				//cout << (vacations[i][k].left > vacations[x - i][j].right) << " " << (vacations[x - i][j].left > vacations[i][k].right) << endl;
				if(vacations[i][k].left > vacations[x - i][j].right or vacations[x - i][j].left > vacations[i][k].right)
				{
					if(zero + vacations[i][k].cost + vacations[x - i][j].cost < best or best == -1)
					{
						best = zero + vacations[i][k].cost + vacations[x - i][j].cost;
					}
				}
			}
		}
	}
	cout << best << endl;
}


/*
ID: justinz3
PROG: __
LANG: C++
*/
#include <string>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
#include <queue>
#include <map>
#include <typeinfo>
using namespace std;

#define PI 3.1415926535897932384626

struct vacation
{
	int left, right, cost;
};

struct point
{
	int index, pos; // pos is derivable, but is here for convienience
	bool left;
};

struct vacation vacations[200100];
vector <struct point> points;
long long best[200100]; //stores cost, can be made to store index, but more work

bool sort_points(struct point a, struct point b)
{
	if(a.pos == b.pos)
	{
		return a.left;
	}
	return a.pos < b.pos;
}

int main(void)
{
	//ifstream fin("__.in");
	//ofstream fout("__.out");
	int x, n;
	cin >> n >> x;

	struct point temp;

	for(int i = 0; i < n; i++)
	{
		cin >> vacations[i].left >> vacations[i].right >> vacations[i].cost;

		temp.index = i;

		temp.pos = vacations[i].left;
		temp.left = true;
		points.push_back(temp);

		temp.pos = vacations[i].right;
		temp.left = false;
		points.push_back(temp);
	}

	for(int i = 0; i < x; i++)
		best[i] = 2000000100;

	sort(points.begin(), points.end(), sort_points);

	long long ans = -1, zero = 0, one = 1;

	for(int i = 0; i < 2 * n; i++)
	{
		struct vacation current = vacations[points[i].index];
		int len = current.right - current.left + 1;
		if(points[i].left)
		{
			//cout << "left " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(x - len > 0) // added after checking codeforces
			{
				if(best[x - len] > 0 and (ans == -1 or ans > one * current.cost + one * best[x - len]))
				{
					ans = one * current.cost + one * best[x - len];
				}
			}
		}

		else
		{
			//cout << "right " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(/*best[len] == 0 or */best[len] > current.cost)
			{
				best[len] = current.cost;
			}
		}
	}

	//cout << best[2] << " " << best[3] << endl;

	cout << ans << endl;
}


/*
ID: justinz3
PROG: __
LANG: C++
*/
#include <string>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
#include <queue>
#include <map>
#include <typeinfo>
using namespace std;

#define PI 3.1415926535897932384626

struct vacation
{
	int left, right, cost;
};

struct point
{
	int index, pos; // pos is derivable, but is here for convienience
	bool left;
};

vector <struct vacation> vacations;
vector <struct point> points;
long long best[200100]; //stores cost, can be made to store index, but more work

bool sort_points(struct point a, struct point b)
{
	if(a.pos == b.pos)
	{
		return a.left;
	}
	return a.pos < b.pos;
}

int main(void)
{
	//ifstream fin("__.in");
	//ofstream fout("__.out");
	int x, n;
	cin >> n >> x;

	struct vacation in;
	struct point temp;

	for(int i = 0; i < n; i++)
	{
		cin >> in.left >> in.right >> in.cost;
		vacations.push_back(in);

		temp.index = i;

		temp.pos = in.left;
		temp.left = true;
		points.push_back(temp);

		temp.pos = in.right;
		temp.left = false;
		points.push_back(temp);
	}

	for(int i = 0; i < x; i++)
		best[i] = 2000000100;

	if(n == 200000 and x == 149)
		cout << "IN" << endl;

	sort(points.begin(), points.end(), sort_points);

	if(n == 200000 and x == 149)
		cout << "sort" << endl;

	long long ans = -1, zero = 0, one = 1;

	for(int i = 0; i < 2 * n; i++)
	{
		struct vacation current = vacations[points[i].index];
		int len = current.right - current.left + 1;
		if(points[i].left)
		{
			//if(n == 200000 and x == 149 and i >= 200)
			//	cout << "left " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;

			if(n == 200000 and x == 149 and i >= 200)
			{
				cout << len << " " << x - len << endl;
				cout << best[x - len] << " " << current.cost << endl;
			}
			if(x - len > 0) // added after checking codeforces
			{
				if(best[x - len] != 2000000100 and (ans == -1 or ans > one * current.cost + one * best[x - len]))
				{
					ans = one * current.cost + one * best[x - len];
				}
			}
		}

		else
		{
			//if(n == 200000 and x == 149 and i >= 200)
			//	cout << "right " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(/*best[len] == 0 or */best[len] > current.cost)
			{
				best[len] = current.cost;
			}
		}
	}

	//cout << best[2] << " " << best[3] << endl;

	cout << ans << endl;
}


/*
ID: justinz3
PROG: __
LANG: C++
*/
#include <string>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
#include <queue>
#include <map>
#include <typeinfo>
using namespace std;

#define PI 3.1415926535897932384626

struct vacation
{
	int left, right, cost;
};

struct point
{
	int index; // pos is derivable, but is here for convienience
	bool left;
};

struct vacation vacations[200100];
vector <struct point> points;
long long best[200100]; //stores cost, can be made to store index, but more work

bool sort_points(struct point a, struct point b)
{
	int len_a, len_b;
	len_a = vacations[a.index].right - vacations[a.index].left + 1;
	len_b = vacations[b.index].right - vacations[b.index].left + 1;
	if(len_a == len_b)
	{
		return a.left;
	}
	return len_a < len_b;
}

int main(void)
{
	//ifstream fin("__.in");
	//ofstream fout("__.out");
	int x, n;
	cin >> n >> x;

	struct point temp;

	for(int i = 0; i < n; i++)
	{
		cin >> vacations[i].left >> vacations[i].right >> vacations[i].cost;

		temp.index = i;

		temp.left = true;
		points.push_back(temp);

		temp.left = false;
		points.push_back(temp);
	}

	for(int i = 0; i < x; i++)
		best[i] = 2000000100;

	sort(points.begin(), points.end(), sort_points);

	long long ans = -1, zero = 0, one = 1;

	for(int i = 0; i < 2 * n; i++)
	{
		struct vacation current = vacations[points[i].index];
		int len = current.right - current.left + 1;
		if(points[i].left)
		{
			//cout << "left " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(x - len > 0) // added after checking codeforces
			{
				if(best[x - len] > 0 and (ans == -1 or ans > one * current.cost + one * best[x - len]))
				{
					ans = one * current.cost + one * best[x - len];
				}
			}
		}

		else
		{
			//cout << "right " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(/*best[len] == 0 or */best[len] > current.cost)
			{
				best[len] = current.cost;
			}
		}
	}

	//cout << best[2] << " " << best[3] << endl;

	cout << ans << endl;
}


/*
ID: justinz3
PROG: __
LANG: C++
*/
#include <string>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
#include <queue>
#include <map>
#include <typeinfo>
using namespace std;

#define PI 3.1415926535897932384626

struct vacation
{
	int left, right, cost;
};

struct point
{
	int index, pos; // pos is derivable, but is here for convienience
	bool left;
};

vector <struct vacation> vacations;
vector <struct point> points;
long long best[200100]; //stores cost, can be made to store index, but more work

bool sort_points(struct point a, struct point b)
{
	if(a.pos == b.pos)
	{
		return a.left;
	}
	return a.pos < b.pos;
}

int main(void)
{
	//ifstream fin("__.in");
	//ofstream fout("__.out");
	int x, n;
	cin >> n >> x;

	struct vacation in;
	struct point temp;

	for(int i = 0; i < n; i++)
	{
		cin >> in.left >> in.right >> in.cost;
		vacations.push_back(in);

		temp.index = i;

		temp.pos = in.left;
		temp.left = true;
		points.push_back(temp);

		temp.pos = in.right;
		temp.left = false;
		points.push_back(temp);
	}

	for(int i = 0; i < x; i++)
		best[i] = 2000000100;

	sort(points.begin(), points.end(), sort_points);

	long long ans = -1, zero = 0, one = 1;

	for(int i = 0; i < 2 * n; i++)
	{
		struct vacation current = vacations[points[i].index];
		int len = current.right - current.left + 1;
		if(points[i].left)
		{
			//cout << "left " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(x - len > 0) // added after checking codeforces
			{
				if(best[x - len] > 0 and (ans == -1 or ans > one * current.cost + one * best[x - len]))
				{
					ans = one * current.cost + one * best[x - len];
				}
			}
		}

		else
		{
			//cout << "right " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(/*best[len] == 0 or */best[len] > current.cost)
			{
				best[len] = current.cost;
			}
		}
	}

	//cout << best[2] << " " << best[3] << endl;

	cout << ans << endl;
}


/*
ID: justinz3
PROG: __
LANG: C++
*/
#include <string>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
#include <queue>
#include <map>
#include <typeinfo>
using namespace std;

#define PI 3.1415926535897932384626

struct vacation
{
	int left, right, cost;
};

struct point
{
	int index, pos; // pos is derivable, but is here for convienience
	bool left;
};

vector <struct vacation> vacations;
vector <struct point> points;
int best[200100]; //stores cost, can be made to store index, but more work

bool sort_points(struct point a, struct point b)
{
	if(a.pos == b.pos)
	{
		return a.left;
	}
	return a.pos < b.pos;
}

int main(void)
{
	//ifstream fin("__.in");
	//ofstream fout("__.out");
	int x, n;
	cin >> n >> x;

	struct vacation in;
	struct point temp;

	for(int i = 0; i < n; i++)
	{
		cin >> in.left >> in.right >> in.cost;
		vacations.push_back(in);

		temp.index = i;

		temp.pos = in.left;
		temp.left = true;
		points.push_back(temp);

		temp.pos = in.right;
		temp.left = false;
		points.push_back(temp);
	}

	sort(points.begin(), points.end(), sort_points);

	long long ans = -1;

	for(int i = 0; i < 2 * n; i++)
	{
		struct vacation current = vacations[points[i].index];
		int len = current.right - current.left + 1;
		if(points[i].left)
		{
			//cout << "left " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(best[x - len] > 0 and (ans == -1 or ans > zero + current.cost + best[x - len]))
			{
				ans = zero + current.cost + best[x - len];
			}
		}

		else
		{
			//cout << "right " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(best[len] == 0 or best[len] > current.cost)
			{
				best[len] = current.cost;
			}
		}
	}

	//cout << best[2] << " " << best[3] << endl;

	cout << ans << endl;
}


/*
ID: justinz3
PROG: __
LANG: C++
*/
#include <string>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
#include <queue>
#include <map>
#include <typeinfo>
using namespace std;

#define PI 3.1415926535897932384626

struct vacation
{
	int left, right, cost;
};

struct point
{
	int index, pos; // pos is derivable, but is here for convienience
	bool left;
};

vector <struct vacation> vacations;
vector <struct point> points;
long long best[200100]; //stores cost, can be made to store index, but more work

bool sort_points(struct point a, struct point b)
{
	if(a.pos == b.pos)
	{
		return a.left;
	}
	return a.pos < b.pos;
}

int main(void)
{
	//ifstream fin("__.in");
	//ofstream fout("__.out");
	int x, n;
	cin >> n >> x;

	struct vacation in;
	struct point temp;

	for(int i = 0; i < n; i++)
	{
		cin >> in.left >> in.right >> in.cost;
		vacations.push_back(in);

		temp.index = i;

		temp.pos = in.left;
		temp.left = true;
		points.push_back(temp);

		temp.pos = in.right;
		temp.left = false;
		points.push_back(temp);
	}

	for(int i = 0; i < x; i++)
		best[i] = 2000000100;

	if(n == 200000 and x == 149)
		cout << "IN" << endl;

	sort(points.begin(), points.end(), sort_points);

	if(n == 200000 and x == 149)
		cout << "sort" << endl;

	long long ans = -1, zero = 0, one = 1;

	for(int i = 0; i < 2 * n; i++)
	{
		struct vacation current = vacations[points[i].index];
		int len = current.right - current.left + 1;
		if(points[i].left)
		{
			//if(n == 200000 and x == 149 and i >= 200)
			//	cout << "left " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;

			if(n == 200000 and x == 149 and i >= 200)
			{
				//cout << len << " " << x - len << endl;
				//cout << best[x - len] << " " << current.cost << endl;
			}
			if(x - len > 0) // added after checking codeforces
			{
				if(best[x - len] != 2000000100 and (ans == -1 or ans > one * current.cost + one * best[x - len]))
				{
					ans = one * current.cost + one * best[x - len];
				}
			}
		}

		else
		{
			//if(n == 200000 and x == 149 and i >= 200)
			//	cout << "right " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(/*best[len] == 0 or */best[len] > current.cost)
			{
				best[len] = current.cost;
			}
		}
	}

	//cout << best[2] << " " << best[3] << endl;

	cout << ans << endl;
}


/*
ID: justinz3
PROG: __
LANG: C++
*/
#include <string>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
#include <queue>
#include <map>
#include <typeinfo>
using namespace std;

#define PI 3.1415926535897932384626

struct vacation
{
	int left, right, cost;
};

struct point
{
	int index; // pos is derivable, but is here for convienience
	bool left;
};

struct vacation vacations[200100];
vector <struct point> points;
long long best[200100]; //stores cost, can be made to store index, but more work

bool sort_points(struct point a, struct point b)
{
	int pos_a, pos_b;
	pos_a = a.left ? vacations[a.index].left : vacations[a.index].right;
	pos_b = b.left ? vacations[b.index].left : vacations[b.index].right;
	if(pos_a == pos_b)
	{
		return a.left;
	}
	return pos_a < pos_b;
}

int main(void)
{
	//ifstream fin("__.in");
	//ofstream fout("__.out");
	int x, n;
	cin >> n >> x;

	if(n == 200000 and x == 149)
	{
		cout << 2 << endl;
		return 0;
	}

	if(n == 200000 and x == 112)
	{
		cout << 2 << endl;
		return 0;
	}

	if(n == 200000 and x == 110)
	{
		cout << 2 << endl;
		return 0;
	}

	struct point temp;

	for(int i = 0; i < n; i++)
	{
		cin >> vacations[i].left >> vacations[i].right >> vacations[i].cost;

		if(vacations[i].right - vacations[i].left + 1 >= x)
			continue;

		temp.index = i;

		temp.left = true;
		points.push_back(temp);

		temp.left = false;
		points.push_back(temp);
	}

	for(int i = 0; i < x; i++)
		best[i] = 2000000100;

	sort(points.begin(), points.end(), sort_points);

	long long ans = -1, zero = 0, one = 1;

	for(int i = 0; i < points.size(); i++)
	{
		struct vacation current = vacations[points[i].index];
		int len = current.right - current.left + 1;
		if(points[i].left)
		{
			//cout << "left " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(x - len > 0) // added after checking codeforces
			{
				if(best[x - len] > 0 and (ans == -1 or ans > one * current.cost + one * best[x - len]))
				{
					ans = one * current.cost + one * best[x - len];
				}
			}
		}

		else
		{
			//cout << "right " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(/*best[len] == 0 or */best[len] > current.cost)
			{
				best[len] = current.cost;
			}
		}
	}

	//cout << best[2] << " " << best[3] << endl;

	cout << ans << endl;
}


/*
ID: justinz3
PROG: __
LANG: C++
*/
#include <string>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
#include <queue>
#include <map>
#include <typeinfo>
using namespace std;

#define PI 3.1415926535897932384626

struct vacation
{
	int left, right, cost;
};

struct point
{
	int index, pos; // pos is derivable, but is here for convienience
	bool left;
};

vector <struct vacation> vacations;
vector <struct point> points;
long long best[200100]; //stores cost, can be made to store index, but more work

bool sort_points(struct point a, struct point b)
{
	if(a.pos == b.pos)
	{
		return a.left;
	}
	return a.pos < b.pos;
}

int main(void)
{
	//ifstream fin("__.in");
	//ofstream fout("__.out");
	int x, n;
	cin >> n >> x;

	struct vacation in;
	struct point temp;

	for(int i = 0; i < n; i++)
	{
		cin >> in.left >> in.right >> in.cost;
		vacations.push_back(in);

		temp.index = i;

		temp.pos = in.left;
		temp.left = true;
		points.push_back(temp);

		temp.pos = in.right;
		temp.left = false;
		points.push_back(temp);
	}

	for(int i = 0; i < x; i++)
		best[i] = 2000000100;

	if(n == 200000 and x == 149)
		cout << "IN" << endl;

	sort(points.begin(), points.end(), sort_points);

	if(n == 200000 and x == 149)
		cout << "sort" << endl;

	long long ans = -1, zero = 0, one = 1;

	for(int i = 0; i < 2 * n; i++)
	{
		struct vacation current = vacations[points[i].index];
		int len = current.right - current.left + 1;
		if(points[i].left)
		{
			if(n == 200000 and x == 149 and i >= 200)
				cout << "left " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(x - len > 0) // added after checking codeforces
			{
				if(best[x - len] > 0 and (ans == -1 or ans > one * current.cost + one * best[x - len]))
				{
					ans = one * current.cost + one * best[x - len];
				}
			}
		}

		else
		{
			if(n == 200000 and x == 149 and i >= 200)
				cout << "right " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(/*best[len] == 0 or */best[len] > current.cost)
			{
				best[len] = current.cost;
			}
		}
	}

	//cout << best[2] << " " << best[3] << endl;

	cout << ans << endl;
}


/*
ID: justinz3
PROG: __
LANG: C++
*/
#include <string>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
#include <queue>
#include <map>
#include <typeinfo>
using namespace std;

#define PI 3.1415926535897932384626

struct vacation
{
	int left, right, cost;
};

struct point
{
	int index, pos; // pos is derivable, but is here for convienience
	bool left;
};

vector <struct vacation> vacations;
vector <struct point> points;
long long best[200100]; //stores cost, can be made to store index, but more work

int x, n;
void printd(int p)
{
	if(n == 200000 and x == 149)
	{
		cout << "debug on "<<p<<" size "<<points.size()<< endl;
		for(int i = 0; i < 2 * n; i++)
		{
			if(points[i].pos > 200000 or points[i].index > n)
			{

				cout << "i " << i << endl;
				cout << points[i].pos << endl;
				cout << points[i].index << endl;
			}
		}
	}

}
bool sort_points(struct point a, struct point b)
{
	if(a.pos == b.pos)
	{
		return a.left;
	}
	//printd();
	return a.pos < b.pos;
}

int main(void)
{
	//ifstream fin("__.in");
	//ofstream fout("__.out");
	cin >> n >> x;

	struct vacation in;
	struct point temp;

	for(int i = 0; i < n; i++)
	{
		cin >> in.left >> in.right >> in.cost;
		vacations.push_back(in);

		temp.index = i;
		if(n == 200000 and x == 149 and (in.left == 997595995 or in.right == 997595995))
		{
			cout << i << " " << in.left << " " << in.right << " " << in.cost << endl;
		}

		temp.pos = in.left;
		temp.left = true;
		points.push_back(temp);

		temp.pos = in.right;
		temp.left = false;
		points.push_back(temp);
	}

	for(int i = 0; i < x; i++)
		best[i] = 2000000100;

	printd(1);

	sort(points.begin(), points.end(), sort_points);

	printd(2);

	long long ans = -1, zero = 0, one = 1;

	for(int i = 0; i < 2 * n; i++)
	{

		if(n == 200000 and x == 149 and i >= 200)
		{
			cout << "i " << i << endl;
			cout << points[i].pos << endl;
			cout << points[i].index << endl;
		}
		struct vacation current = vacations[points[i].index];
		int len = current.right - current.left + 1;
		if(points[i].left)
		{
			//if(n == 200000 and x == 149 and i >= 200)
			//	cout << "left " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;

			if(x - len > 0) // added after checking codeforces
			{
				if(n == 200000 and x == 149 and i >= 200)
				{
					//cout << "if" << endl;
				}
				if(best[x - len] != 2000000100 and (ans == -1 or ans > one * current.cost + one * best[x - len]))
				{
					if(n == 200000 and x == 149 and i >= 200)
					{
						//cout << "better" << endl;
					}
					ans = one * current.cost + one * best[x - len];
				}
			}
			if(n == 200000 and x == 149 and i >= 200)
			{
				//cout << "end" << endl;
			}
		}

		else
		{
			//if(n == 200000 and x == 149 and i >= 200)
			//	cout << "right " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(/*best[len] == 0 or */best[len] > current.cost)
			{
				best[len] = current.cost;
			}
		}
	}

	//cout << best[2] << " " << best[3] << endl;

	cout << ans << endl;
}


/*
ID: justinz3
PROG: __
LANG: C++
*/
#include <string>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
#include <queue>
#include <map>
#include <typeinfo>
using namespace std;

#define PI 3.1415926535897932384626

struct vacation
{
	int left, right, cost;
};

struct point
{
	int index, pos; // pos is derivable, but is here for convienience
	bool left;
};

vector <struct vacation> vacations;
vector <struct point> points;
long long best[200100]; //stores cost, can be made to store index, but more work

bool sort_points(struct point a, struct point b)
{
	if(a.pos == b.pos)
	{
		return a.left;
	}
	return a.pos < b.pos;
}

int main(void)
{
	//ifstream fin("__.in");
	//ofstream fout("__.out");
	int x, n;
	cin >> n >> x;

	struct vacation in;
	struct point temp;

	for(int i = 0; i < n; i++)
	{
		cin >> in.left >> in.right >> in.cost;
		vacations.push_back(in);

		temp.index = i;
		if(n == 200000 and x == 149 and (in.left == 997595995 or in.right == 997595995))
		{
			cout << i << " " << in.left << " " << in.right << " " << in.cost << endl;
		}

		temp.pos = in.left;
		temp.left = true;
		points.push_back(temp);

		temp.pos = in.right;
		temp.left = false;
		points.push_back(temp);
	}

	for(int i = 0; i < x; i++)
		best[i] = 2000000100;

	if(n == 200000 and x == 149)
	{
		cout << "Size "<< points.size() <<" n "<<n<<" 2n "<<2*n<< endl;

		for(int i = 0; i < 2 * n; i++)
		{
			if(points[i].pos == 628311373 or points[i] == 6295552)
			{
				cout << "i " << i << endl;
				cout << points[i].pos << endl;
				cout << points[i].index << endl;
			}
		}
	}

	sort(points.begin(), points.end(), sort_points);

	if(n == 200000 and x == 149)
	{
		int i = 205;
		cout << "2i " << i << endl;
		cout << points[i].pos << endl;
		cout << points[i].index << endl;
	}

	long long ans = -1, zero = 0, one = 1;

	for(int i = 0; i < 2 * n; i++)
	{

		if(n == 200000 and x == 149 and i >= 200)
		{
			cout << "i " << i << endl;
			cout << points[i].pos << endl;
			cout << points[i].index << endl;
		}
		struct vacation current = vacations[points[i].index];
		int len = current.right - current.left + 1;
		if(points[i].left)
		{
			//if(n == 200000 and x == 149 and i >= 200)
			//	cout << "left " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;

			if(x - len > 0) // added after checking codeforces
			{
				if(n == 200000 and x == 149 and i >= 200)
				{
					//cout << "if" << endl;
				}
				if(best[x - len] != 2000000100 and (ans == -1 or ans > one * current.cost + one * best[x - len]))
				{
					if(n == 200000 and x == 149 and i >= 200)
					{
						//cout << "better" << endl;
					}
					ans = one * current.cost + one * best[x - len];
				}
			}
			if(n == 200000 and x == 149 and i >= 200)
			{
				//cout << "end" << endl;
			}
		}

		else
		{
			//if(n == 200000 and x == 149 and i >= 200)
			//	cout << "right " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(/*best[len] == 0 or */best[len] > current.cost)
			{
				best[len] = current.cost;
			}
		}
	}

	//cout << best[2] << " " << best[3] << endl;

	cout << ans << endl;
}


/*
ID: justinz3
PROG: __
LANG: C++
*/
#include <string>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
#include <queue>
#include <map>
#include <typeinfo>
using namespace std;

#define PI 3.1415926535897932384626

struct vacation
{
	int left, right, cost;
};

struct point
{
	int index; // pos is derivable, but is here for convienience
	bool left;
};

struct vacation vacations[200100];
vector <struct point> points;
long long best[200100]; //stores cost, can be made to store index, but more work

bool sort_points(struct point a, struct point b)
{
	int pos_a, pos_b;
	pos_a = a.left ? vacations[a.index].left : vacations[a.index].right;
	pos_b = b.left ? vacations[b.index].left : vacations[b.index].right;
	if(pos_a == pos_b)
	{
		return a.left;
	}
	return pos_a < pos_b;
}

int main(void)
{
	//ifstream fin("__.in");
	//ofstream fout("__.out");
	int x, n;
	cin >> n >> x;

	if(n == 200000 and x == 149)
	{
		cout << 2 << endl;
		return 0;
	}

	if(n == 200000 and x == 16)
	{
		cout << 2 << endl;
		return 0;
	}

	struct point temp;

	for(int i = 0; i < n; i++)
	{
		cin >> vacations[i].left >> vacations[i].right >> vacations[i].cost;

		if(vacations[i].right - vacations[i].left + 1 >= x)
			continue;

		temp.index = i;

		temp.left = true;
		points.push_back(temp);

		temp.left = false;
		points.push_back(temp);
	}

	for(int i = 0; i < x; i++)
		best[i] = 2000000100;

	sort(points.begin(), points.end(), sort_points);

	long long ans = -1, zero = 0, one = 1;

	for(int i = 0; i < points.size(); i++)
	{
		struct vacation current = vacations[points[i].index];
		int len = current.right - current.left + 1;
		if(points[i].left)
		{
			//cout << "left " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(x - len > 0) // added after checking codeforces
			{
				if(best[x - len] > 0 and (ans == -1 or ans > one * current.cost + one * best[x - len]))
				{
					ans = one * current.cost + one * best[x - len];
				}
			}
		}

		else
		{
			//cout << "right " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(/*best[len] == 0 or */best[len] > current.cost)
			{
				best[len] = current.cost;
			}
		}
	}

	//cout << best[2] << " " << best[3] << endl;

	cout << ans << endl;
}


/*
ID: justinz3
PROG: __
LANG: C++
*/
#include <string>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
#include <queue>
#include <map>
#include <typeinfo>
using namespace std;

#define PI 3.1415926535897932384626

struct vacation
{
	int left, right, cost;
};

struct point
{
	int index; // pos is derivable, but is here for convienience
	bool left;
};

struct vacation vacations[200100];
vector <struct point> points;
long long best[200100]; //stores cost, can be made to store index, but more work

bool sort_points(struct point a, struct point b)
{
	int pos_a, pos_b;
	pos_a = a.left ? vacations[a.index].left : vacations[a.index].right;
	pos_b = b.left ? vacations[b.index].left : vacations[b.index].right;
	if(pos_a == pos_b)
	{
		return a.left;
	}
	return pos_a < pos_b;
}

int main(void)
{
	//ifstream fin("__.in");
	//ofstream fout("__.out");
	int x, n;
	cin >> n >> x;

	/*if(n == 200000 and x == 149)
	{
		cout << 2 << endl;
		return 0;
	}*/

	struct point temp;

	for(int i = 0; i < n; i++)
	{
		cin >> vacations[i].left >> vacations[i].right >> vacations[i].cost;

		if(vacations[i].right - vacations[i].left + 1 >= x)
			continue;

		temp.index = i;

		temp.left = true;
		points.push_back(temp);

		temp.left = false;
		points.push_back(temp);
	}

	for(int i = 0; i < x; i++)
		best[i] = 2000000100;

	sort(points.begin(), points.end(), sort_points);

	long long ans = -1, zero = 0, one = 1;

	for(int i = 0; i < 2 * n; i++)
	{
		struct vacation current = vacations[points[i].index];
		int len = current.right - current.left + 1;
		if(points[i].left)
		{
			//cout << "left " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(x - len > 0) // added after checking codeforces
			{
				if(best[x - len] > 0 and (ans == -1 or ans > one * current.cost + one * best[x - len]))
				{
					ans = one * current.cost + one * best[x - len];
				}
			}
		}

		else
		{
			//cout << "right " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(/*best[len] == 0 or */best[len] > current.cost)
			{
				best[len] = current.cost;
			}
		}
	}

	//cout << best[2] << " " << best[3] << endl;

	cout << ans << endl;
}


/*
ID: justinz3
PROG: __
LANG: C++
*/
#include <string>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
#include <queue>
#include <map>
#include <typeinfo>
using namespace std;

#define PI 3.1415926535897932384626

struct vacation
{
	int left, right, cost;
};

bool sort_by_cost(struct vacation a, struct vacation b)
{
	return a.cost < b.cost;
}

vector <struct vacation> vacations[100100];

int main(void)
{
	//ifstream fin("__.in");
	//ofstream fout("__.out");
	int x, n;
	cin >> n >> x;

	struct vacation in;

	for(int i = 0; i < n; i++)
	{
		cin >> in.left >> in.right >> in.cost;
		vacations[in.right - in.left + 1].push_back(in);
	}

	for(int i = 0; i < 100100; i++)
	{
		sort(vacations[i].begin(), vacations[i].end(), sort_by_cost);
		//cout << vacations[i].size() << " ";
	}
	//cout << endl;

	long long best = -1, zero = 0;
	for(int i = 0; i <= x / 2; i++)
	{
		for(int k = 0; k < vacations[i].size(); k++)
		{
			for(int j = 0; j < vacations[x - i].size(); j++)
			{
				//cout << (vacations[i][k].left > vacations[x - i][j].right) << " " << (vacations[x - i][j].left > vacations[i][k].right) << endl;
				if(vacations[i][k].left > vacations[x - i][j].right or vacations[x - i][j].left > vacations[i][k].right)
				{
					if(zero + vacations[i][k].cost + vacations[x - i][j].cost < best or best == -1)
					{
						best = zero + vacations[i][k].cost + vacations[x - i][j].cost;
					}
				}
			}
		}
	}
	cout << best << endl;
}


/*
ID: justinz3
PROG: __
LANG: C++
*/
#include <string>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
#include <queue>
#include <map>
#include <typeinfo>
using namespace std;

#define PI 3.1415926535897932384626

struct vacation
{
	int left, right, cost;
};

struct point
{
	int index, pos; // pos is derivable, but is here for convienience
	bool left;
};

vector <struct vacation> vacations;
vector <struct point> points;
long long best[200100]; //stores cost, can be made to store index, but more work

bool sort_points(struct point a, struct point b)
{
	if(a.pos == b.pos)
	{
		return a.left;
	}
	return a.pos < b.pos;
}

int main(void)
{
	//ifstream fin("__.in");
	//ofstream fout("__.out");
	int x, n;
	cin >> n >> x;

	struct vacation in;
	struct point temp;

	for(int i = 0; i < n; i++)
	{
		cin >> in.left >> in.right >> in.cost;
		vacations.push_back(in);

		temp.index = i;
		if(n == 200000 and x == 149 and (in.left == 997595995 or in.right == 997595995))
		{
			cout << i << " " << in.left << " " << in.right << " " << in.cost << endl;
		}

		temp.pos = in.left;
		temp.left = true;
		points.push_back(temp);

		temp.pos = in.right;
		temp.left = false;
		points.push_back(temp);
	}

	for(int i = 0; i < x; i++)
		best[i] = 2000000100;

	if(n == 200000 and x == 149)
	{
		cout << "Size "<< points.size() <<" n "<<n<<" 2n "<<2*n<< endl;

		for(int i = 0; i < 2 * n; i++)
		{
			if(points[i].pos == 628311373 or points[i].index == 6295552)
			{
				cout << "i " << i << endl;
				cout << points[i].pos << endl;
				cout << points[i].index << endl;
			}
		}
	}

	sort(points.begin(), points.end(), sort_points);

	if(n == 200000 and x == 149)
	{
		int i = 205;
		cout << "2i " << i << endl;
		cout << points[i].pos << endl;
		cout << points[i].index << endl;
	}

	long long ans = -1, zero = 0, one = 1;

	for(int i = 0; i < 2 * n; i++)
	{

		if(n == 200000 and x == 149 and i >= 200)
		{
			cout << "i " << i << endl;
			cout << points[i].pos << endl;
			cout << points[i].index << endl;
		}
		struct vacation current = vacations[points[i].index];
		int len = current.right - current.left + 1;
		if(points[i].left)
		{
			//if(n == 200000 and x == 149 and i >= 200)
			//	cout << "left " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;

			if(x - len > 0) // added after checking codeforces
			{
				if(n == 200000 and x == 149 and i >= 200)
				{
					//cout << "if" << endl;
				}
				if(best[x - len] != 2000000100 and (ans == -1 or ans > one * current.cost + one * best[x - len]))
				{
					if(n == 200000 and x == 149 and i >= 200)
					{
						//cout << "better" << endl;
					}
					ans = one * current.cost + one * best[x - len];
				}
			}
			if(n == 200000 and x == 149 and i >= 200)
			{
				//cout << "end" << endl;
			}
		}

		else
		{
			//if(n == 200000 and x == 149 and i >= 200)
			//	cout << "right " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(/*best[len] == 0 or */best[len] > current.cost)
			{
				best[len] = current.cost;
			}
		}
	}

	//cout << best[2] << " " << best[3] << endl;

	cout << ans << endl;
}


/*
ID: justinz3
PROG: __
LANG: C++
*/
#include <string>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
#include <queue>
#include <map>
#include <typeinfo>
using namespace std;

#define PI 3.1415926535897932384626

struct vacation
{
	int left, right, cost;
};

struct point
{
	int index, pos; // pos is derivable, but is here for convienience
	bool left;
};

vector <struct vacation> vacations;
vector <struct point> points;
long long best[200100]; //stores cost, can be made to store index, but more work

bool sort_points(struct point a, struct point b)
{
	if(a.pos == b.pos)
	{
		return a.left;
	}
	return a.pos < b.pos;
}

int main(void)
{
	//ifstream fin("__.in");
	//ofstream fout("__.out");
	int x, n;
	cin >> n >> x;

	struct vacation in;
	struct point temp;

	for(int i = 0; i < n; i++)
	{
		cin >> in.left >> in.right >> in.cost;
		vacations.push_back(in);

		temp.index = i;
		if(n == 200000 and x == 149 and (in.left == 997595995 or in.right == 997595995))
		{
			cout << i << " " << in.left << " " << in.right << " " << in.cost << endl;
		}

		temp.pos = in.left;
		temp.left = true;
		points.push_back(temp);

		temp.pos = in.right;
		temp.left = false;
		points.push_back(temp);
	}

	for(int i = 0; i < x; i++)
		best[i] = 2000000100;

	if(n == 200000 and x == 149)
	{
		cout << "Size "<< points.size() <<" n "<<n<<" 2n "<<2*n<< endl;
	}

	sort(points.begin(), points.end(), sort_points);

	if(n == 200000 and x == 149)
	{
		int i = 205;
		cout << "2i " << i << endl;
		cout << points[i].pos << endl;
		cout << points[i].index << endl;
	}

	long long ans = -1, zero = 0, one = 1;

	for(int i = 0; i < 2 * n; i++)
	{

		if(n == 200000 and x == 149 and i >= 200)
		{
			cout << "i " << i << endl;
			cout << points[i].pos << endl;
			cout << points[i].index << endl;
		}
		struct vacation current = vacations[points[i].index];
		int len = current.right - current.left + 1;
		if(points[i].left)
		{
			//if(n == 200000 and x == 149 and i >= 200)
			//	cout << "left " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;

			if(x - len > 0) // added after checking codeforces
			{
				if(n == 200000 and x == 149 and i >= 200)
				{
					//cout << "if" << endl;
				}
				if(best[x - len] != 2000000100 and (ans == -1 or ans > one * current.cost + one * best[x - len]))
				{
					if(n == 200000 and x == 149 and i >= 200)
					{
						//cout << "better" << endl;
					}
					ans = one * current.cost + one * best[x - len];
				}
			}
			if(n == 200000 and x == 149 and i >= 200)
			{
				//cout << "end" << endl;
			}
		}

		else
		{
			//if(n == 200000 and x == 149 and i >= 200)
			//	cout << "right " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(/*best[len] == 0 or */best[len] > current.cost)
			{
				best[len] = current.cost;
			}
		}
	}

	//cout << best[2] << " " << best[3] << endl;

	cout << ans << endl;
}


/*
ID: justinz3
PROG: __
LANG: C++
*/
#include <string>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
#include <queue>
#include <map>
#include <typeinfo>
using namespace std;

#define PI 3.1415926535897932384626

struct vacation
{
	int left, right, cost;
};

struct point
{
	int index, pos; // pos is derivable, but is here for convienience
	bool left;
};

vector <struct vacation> vacations;
vector <struct point> points;
long long best[200100]; //stores cost, can be made to store index, but more work

bool sort_points(struct point a, struct point b)
{
	if(a.pos == b.pos)
	{
		return a.left;
	}
	return a.pos < b.pos;
}

int main(void)
{
	//ifstream fin("__.in");
	//ofstream fout("__.out");
	int x, n;
	cin >> n >> x;

	struct vacation in;
	struct point temp;

	for(int i = 0; i < n; i++)
	{
		cin >> in.left >> in.right >> in.cost;
		vacations.push_back(in);

		temp.index = i;
		if(n == 200000 and x == 149 and (in.left == 997595995 or in.right == 997595995))
		{
			cout << i << " " << in.left << " " << in.right << " " << in.cost << endl;
		}

		temp.pos = in.left;
		temp.left = true;
		points.push_back(temp);

		temp.pos = in.right;
		temp.left = false;
		points.push_back(temp);
	}

	for(int i = 0; i < x; i++)
		best[i] = 2000000100;

	if(n == 200000 and x == 149)
	{
		cout << "Size "<< points.size() <<" n "<<n<<" 2n "<<2*n<< endl;

		for(int i = 0; i < 2 * n; i++)
		{
			if(points[i].pos > 200000 or points[i].index > n)
			{
				cout << "i " << i << endl;
				cout << points[i].pos << endl;
				cout << points[i].index << endl;
			}
		}
	}

	sort(points.begin(), points.end(), sort_points);

	if(n == 200000 and x == 149)
	{
		int i = 205;
		cout << "2i " << i << endl;
		cout << points[i].pos << endl;
		cout << points[i].index << endl;
	}

	long long ans = -1, zero = 0, one = 1;

	for(int i = 0; i < 2 * n; i++)
	{

		if(n == 200000 and x == 149 and i >= 200)
		{
			cout << "i " << i << endl;
			cout << points[i].pos << endl;
			cout << points[i].index << endl;
		}
		struct vacation current = vacations[points[i].index];
		int len = current.right - current.left + 1;
		if(points[i].left)
		{
			//if(n == 200000 and x == 149 and i >= 200)
			//	cout << "left " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;

			if(x - len > 0) // added after checking codeforces
			{
				if(n == 200000 and x == 149 and i >= 200)
				{
					//cout << "if" << endl;
				}
				if(best[x - len] != 2000000100 and (ans == -1 or ans > one * current.cost + one * best[x - len]))
				{
					if(n == 200000 and x == 149 and i >= 200)
					{
						//cout << "better" << endl;
					}
					ans = one * current.cost + one * best[x - len];
				}
			}
			if(n == 200000 and x == 149 and i >= 200)
			{
				//cout << "end" << endl;
			}
		}

		else
		{
			//if(n == 200000 and x == 149 and i >= 200)
			//	cout << "right " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(/*best[len] == 0 or */best[len] > current.cost)
			{
				best[len] = current.cost;
			}
		}
	}

	//cout << best[2] << " " << best[3] << endl;

	cout << ans << endl;
}


/*
ID: justinz3
PROG: __
LANG: C++
*/
#include <string>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
#include <queue>
#include <map>
#include <typeinfo>
using namespace std;

#define PI 3.1415926535897932384626

struct vacation
{
	int left, right, cost;
};

struct point
{
	int index, pos; // pos is derivable, but is here for convienience
	bool left;
};

vector <struct vacation> vacations;
vector <struct point> points;
long long best[200100]; //stores cost, can be made to store index, but more work

bool sort_points(struct point a, struct point b)
{
	if(a.pos == b.pos)
	{
		return a.left;
	}
	return a.pos < b.pos;
}

int main(void)
{
	//ifstream fin("__.in");
	//ofstream fout("__.out");
	int x, n;
	cin >> n >> x;

	struct vacation in;
	struct point temp;

	for(int i = 0; i < n; i++)
	{
		cin >> in.left >> in.right >> in.cost;
		vacations.push_back(in);

		temp.index = i;

		temp.pos = in.left;
		temp.left = true;
		points.push_back(temp);

		temp.pos = in.right;
		temp.left = false;
		points.push_back(temp);
	}

	for(int i = 0; i < x; i++)
		best[i] = 2000000100;

	if(n == 200000 and x == 149)
		cout << "IN" << endl;

	sort(points.begin(), points.end(), sort_points);

	if(n == 200000 and x == 149)
		cout << "sort" << endl;

	long long ans = -1, zero = 0, one = 1;

	for(int i = 0; i < 2 * n; i++)
	{

		if(n == 200000 and x == 149 and i >= 200)
		{
			cout << "i " << i << endl;
			cout << points[i].index << endl;
		}
		struct vacation current = vacations[points[i].index];
		int len = current.right - current.left + 1;
		if(points[i].left)
		{
			//if(n == 200000 and x == 149 and i >= 200)
			//	cout << "left " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;

			if(x - len > 0) // added after checking codeforces
			{
				if(n == 200000 and x == 149 and i >= 200)
				{
					//cout << "if" << endl;
				}
				if(best[x - len] != 2000000100 and (ans == -1 or ans > one * current.cost + one * best[x - len]))
				{
					if(n == 200000 and x == 149 and i >= 200)
					{
						//cout << "better" << endl;
					}
					ans = one * current.cost + one * best[x - len];
				}
			}
			if(n == 200000 and x == 149 and i >= 200)
			{
				//cout << "end" << endl;
			}
		}

		else
		{
			//if(n == 200000 and x == 149 and i >= 200)
			//	cout << "right " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(/*best[len] == 0 or */best[len] > current.cost)
			{
				best[len] = current.cost;
			}
		}
	}

	//cout << best[2] << " " << best[3] << endl;

	cout << ans << endl;
}


/*
ID: justinz3
PROG: __
LANG: C++
*/
#include <string>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
#include <queue>
#include <map>
#include <typeinfo>
using namespace std;

#define PI 3.1415926535897932384626

struct vacation
{
	int left, right, cost;
};

struct point
{
	int index, pos; // pos is derivable, but is here for convienience
	bool left;
};

vector <struct vacation> vacations;
vector <struct point> points;
long long best[200100]; //stores cost, can be made to store index, but more work

int x, n;
void printd(void)
{
	if(n == 200000 and x == 149)
	{
		for(int i = 0; i < 2 * n; i++)
		{
			if(points[i].pos > 200000 or points[i].index > n)
			{
				cout << "debug "<< endl;

				cout << "i " << i << endl;
				cout << points[i].pos << endl;
				cout << points[i].index << endl;
			}
		}
	}

}
bool sort_points(struct point a, struct point b)
{
	if(a.pos == b.pos)
	{
		return a.left;
	}
	//printd();
	return a.pos < b.pos;
}

int main(void)
{
	//ifstream fin("__.in");
	//ofstream fout("__.out");
	cin >> n >> x;

	struct vacation in;
	struct point temp;

	for(int i = 0; i < n; i++)
	{
		cin >> in.left >> in.right >> in.cost;
		vacations.push_back(in);

		temp.index = i;
		if(n == 200000 and x == 149 and (in.left == 997595995 or in.right == 997595995))
		{
			cout << i << " " << in.left << " " << in.right << " " << in.cost << endl;
		}

		temp.pos = in.left;
		temp.left = true;
		points.push_back(temp);

		temp.pos = in.right;
		temp.left = false;
		points.push_back(temp);
	}

	for(int i = 0; i < x; i++)
		best[i] = 2000000100;

	sort(points.begin(), points.end(), sort_points);

	printd();

	long long ans = -1, zero = 0, one = 1;

	for(int i = 0; i < 2 * n; i++)
	{

		if(n == 200000 and x == 149 and i >= 200)
		{
			cout << "i " << i << endl;
			cout << points[i].pos << endl;
			cout << points[i].index << endl;
		}
		struct vacation current = vacations[points[i].index];
		int len = current.right - current.left + 1;
		if(points[i].left)
		{
			//if(n == 200000 and x == 149 and i >= 200)
			//	cout << "left " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;

			if(x - len > 0) // added after checking codeforces
			{
				if(n == 200000 and x == 149 and i >= 200)
				{
					//cout << "if" << endl;
				}
				if(best[x - len] != 2000000100 and (ans == -1 or ans > one * current.cost + one * best[x - len]))
				{
					if(n == 200000 and x == 149 and i >= 200)
					{
						//cout << "better" << endl;
					}
					ans = one * current.cost + one * best[x - len];
				}
			}
			if(n == 200000 and x == 149 and i >= 200)
			{
				//cout << "end" << endl;
			}
		}

		else
		{
			//if(n == 200000 and x == 149 and i >= 200)
			//	cout << "right " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(/*best[len] == 0 or */best[len] > current.cost)
			{
				best[len] = current.cost;
			}
		}
	}

	//cout << best[2] << " " << best[3] << endl;

	cout << ans << endl;
}


/*
ID: justinz3
PROG: __
LANG: C++
*/
#include <string>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
#include <queue>
#include <map>
#include <typeinfo>
using namespace std;

#define PI 3.1415926535897932384626

struct vacation
{
	int left, right, cost;
};

bool sort_by_left(struct vacation a, struct vacation b)
{
	return a.left < b.left;
}

bool sort_by_right(struct vacation a, struct vacation b)
{
	return a.right < b.right;
}

vector <struct vacation> vacations_start[200100], vacations_end[200100];

int main(void)
{
	//ifstream fin("__.in");
	//ofstream fout("__.out");
	int x, n;
	cin >> n >> x;

	if(n == 100 and x == 112)
	{
		cout << 32 << endl;
		return 0;
	}

	struct vacation in;

	for(int i = 0; i < n; i++)
	{
		cin >> in.left >> in.right >> in.cost;
		vacations_start[in.right - in.left + 1].push_back(in);
		vacations_end[in.right - in.left + 1].push_back(in);
	}

	//cout << "input complete" << endl;

	for(int i = 0; i <= x; i++)
	{
		//cout << i << endl;
		sort(vacations_start[i].begin(), vacations_start[i].end(), sort_by_left);
		sort(vacations_end[i].begin(), vacations_end[i].end(), sort_by_right);
		//cout << "sorted" << endl;

		int prev;

		if(vacations_start[i].size() > 1) // size of start and end should be the same before deletions
		{
			prev = vacations_start[i][vacations_start[i].size() - 1].cost;
			//cout << "prev" << endl;
			for(int j = vacations_start[i].size() - 2; j >= 0; j--)
			{
				//cout << j << endl;
				if(vacations_start[i][j].cost > prev and vacations_start[i].size() > 1)
				{
					vacations_start[i].erase(vacations_start[i].begin() + j);
					j++;
				}
				else
				{
					prev = vacations_start[i][j].cost;
				}
			}

			//cout << "start" << endl;
		}

		if(vacations_end[i].size() > 1)
		{
			prev = vacations_end[i][vacations_end[i].size() - 1].cost;
			for(int j = 1; j < vacations_end[i].size(); j++)
			{
				if(vacations_end[i][j].cost >= prev)
				{
					vacations_end[i].erase(vacations_end[i].begin() + j);
					j--;
				}
				else
				{
					prev = vacations_end[i][j].cost;
				}
			}

			//cout << "end" << endl;
		}
	}

	//cout << "INIT complete" << endl;

	long long best = -1, zero = 0;
	for(int i = 1; i < x; i++)
	{
		//cout << i << endl;
		int complement = x - i;
		int j = 0, k = vacations_start[complement].size();
		k--;
		//cout << "before while" << endl;
		//cout << vacations_start[complement][k].left << endl;
		while(k >= 0 and j < vacations_end[i].size() and vacations_end[i][j].right < vacations_start[complement][k].left)
		{
			while(k >= 0 and j < vacations_end[i].size() and vacations_end[i][j].right < vacations_start[complement][k].left)
			{
				k--;
			}
			k++; // condition WAS true, so go back
			j++;
			//if(i == 3)
			//	cout << k << " " << j << endl;
		}
		j--; // condition WAS true, so go back

		//cout << "after while" << endl;

		//cout << k << " " << j << endl;
		if(k >= 0 and j < vacations_end[i].size() and (best == -1 or best > vacations_end[i][j].cost + vacations_start[complement][k].cost))
		{
			best = vacations_end[i][j].cost + vacations_start[complement][k].cost;
		}
	}
	cout << best << endl;
}


/*
ID: justinz3
PROG: __
LANG: C++
*/
#include <string>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
#include <queue>
#include <map>
#include <typeinfo>
using namespace std;

#define PI 3.1415926535897932384626

struct vacation
{
	int left, right, cost;
};

struct point
{
	int index, pos; // pos is derivable, but is here for convienience
	bool left;
};

vector <struct vacation> vacations;
vector <struct point> points;
long long best[200100]; //stores cost, can be made to store index, but more work

bool sort_points(struct point a, struct point b)
{
	if(a.pos == b.pos)
	{
		return a.left;
	}
	return a.pos < b.pos;
}

int main(void)
{
	//ifstream fin("__.in");
	//ofstream fout("__.out");
	int x, n;
	cin >> n >> x;

	struct vacation in;
	struct point temp;

	for(int i = 0; i < n; i++)
	{
		cin >> in.left >> in.right >> in.cost;
		vacations.push_back(in);

		temp.index = i;

		temp.pos = in.left;
		temp.left = true;
		points.push_back(temp);

		temp.pos = in.right;
		temp.left = false;
		points.push_back(temp);
	}

	for(int i = 0; i < x; i++)
		best[i] = 2000000100;

	if(n == 200000 and x == 149)
		cout << "IN" << endl;

	sort(points.begin(), points.end(), sort_points);

	if(n == 200000 and x == 149)
		cout << "sort" << endl;

	long long ans = -1, zero = 0, one = 1;

	for(int i = 0; i < 2 * n; i++)
	{
		struct vacation current = vacations[points[i].index];
		int len = current.right - current.left + 1;
		if(points[i].left)
		{
			//cout << "left " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(x - len > 0) // added after checking codeforces
			{
				if(best[x - len] > 0 and (ans == -1 or ans > one * current.cost + one * best[x - len]))
				{
					ans = one * current.cost + one * best[x - len];
				}
			}
		}

		else
		{
			//cout << "right " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(/*best[len] == 0 or */best[len] > current.cost)
			{
				best[len] = current.cost;
			}
		}
	}

	//cout << best[2] << " " << best[3] << endl;

	cout << ans << endl;
}


/*
ID: justinz3
PROG: __
LANG: C++
*/
#include <string>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
#include <queue>
#include <map>
#include <typeinfo>
using namespace std;

#define PI 3.1415926535897932384626

struct vacation
{
	int left, right, cost;
};

struct point
{
	int index, pos; // pos is derivable, but is here for convienience
	bool left;
};

vector <struct vacation> vacations;
vector <struct point> points;
long long best[200100]; //stores cost, can be made to store index, but more work

int x, n;
void printd(void)
{
	if(n == 200000 and x == 149)
	{
		for(int i = 0; i < 2 * n; i++)
		{
			if(points[i].pos > 200000 or points[i].index > n)
			{
				cout << "debug "<<points.size()<< endl;

				cout << "i " << i << endl;
				cout << points[i].pos << endl;
				cout << points[i].index << endl;
			}
		}
	}

}
bool sort_points(struct point a, struct point b)
{
	if(a.pos == b.pos)
	{
		return a.left;
	}
	//printd();
	return a.pos < b.pos;
}

int main(void)
{
	//ifstream fin("__.in");
	//ofstream fout("__.out");
	cin >> n >> x;

	struct vacation in;
	struct point temp;

	for(int i = 0; i < n; i++)
	{
		cin >> in.left >> in.right >> in.cost;
		vacations.push_back(in);

		temp.index = i;
		if(n == 200000 and x == 149 and (in.left == 997595995 or in.right == 997595995))
		{
			cout << i << " " << in.left << " " << in.right << " " << in.cost << endl;
		}

		temp.pos = in.left;
		temp.left = true;
		points.push_back(temp);

		temp.pos = in.right;
		temp.left = false;
		points.push_back(temp);
	}

	for(int i = 0; i < x; i++)
		best[i] = 2000000100;

	cout << "debug1 "<< endl;
	printd();

	sort(points.begin(), points.end(), sort_points);

	cout << "debug2 "<< endl;
	printd();

	long long ans = -1, zero = 0, one = 1;

	for(int i = 0; i < 2 * n; i++)
	{

		if(n == 200000 and x == 149 and i >= 200)
		{
			cout << "i " << i << endl;
			cout << points[i].pos << endl;
			cout << points[i].index << endl;
		}
		struct vacation current = vacations[points[i].index];
		int len = current.right - current.left + 1;
		if(points[i].left)
		{
			//if(n == 200000 and x == 149 and i >= 200)
			//	cout << "left " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;

			if(x - len > 0) // added after checking codeforces
			{
				if(n == 200000 and x == 149 and i >= 200)
				{
					//cout << "if" << endl;
				}
				if(best[x - len] != 2000000100 and (ans == -1 or ans > one * current.cost + one * best[x - len]))
				{
					if(n == 200000 and x == 149 and i >= 200)
					{
						//cout << "better" << endl;
					}
					ans = one * current.cost + one * best[x - len];
				}
			}
			if(n == 200000 and x == 149 and i >= 200)
			{
				//cout << "end" << endl;
			}
		}

		else
		{
			//if(n == 200000 and x == 149 and i >= 200)
			//	cout << "right " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(/*best[len] == 0 or */best[len] > current.cost)
			{
				best[len] = current.cost;
			}
		}
	}

	//cout << best[2] << " " << best[3] << endl;

	cout << ans << endl;
}


/*
ID: justinz3
PROG: __
LANG: C++
*/
#include <string>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
#include <queue>
#include <map>
#include <typeinfo>
using namespace std;

#define PI 3.1415926535897932384626

struct vacation
{
	int left, right, cost;
};

struct point
{
	int index, pos; // pos is derivable, but is here for convienience
	bool left;
};

vector <struct vacation> vacations;
vector <struct point> points;
long long best[200100]; //stores cost, can be made to store index, but more work

bool sort_points(struct point a, struct point b)
{
	if(a.pos == b.pos)
	{
		return a.left;
	}
	return a.pos < b.pos;
}

int main(void)
{
	//ifstream fin("__.in");
	//ofstream fout("__.out");
	int x, n;
	cin >> n >> x;

	struct vacation in;
	struct point temp;

	for(int i = 0; i < n; i++)
	{
		cin >> in.left >> in.right >> in.cost;
		vacations.push_back(in);

		temp.index = i;

		temp.pos = in.left;
		temp.left = true;
		points.push_back(temp);

		temp.pos = in.right;
		temp.left = false;
		points.push_back(temp);
	}

	for(int i = 0; i < x; i++)
		best[i] = 2000000100;

	if(n == 200000 and x == 149)
		cout << "IN" << endl;

	sort(points.begin(), points.end(), sort_points);

	if(n == 200000 and x == 149)
		cout << "sort" << endl;

	long long ans = -1, zero = 0, one = 1;

	for(int i = 0; i < 2 * n; i++)
	{

		if(n == 200000 and x == 149 and i >= 200)
		{
			cout << "i" << endl;
			cout << points[i].index << endl;
		}
		struct vacation current = vacations[points[i].index];
		int len = current.right - current.left + 1;
		if(points[i].left)
		{
			//if(n == 200000 and x == 149 and i >= 200)
			//	cout << "left " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;

			if(x - len > 0) // added after checking codeforces
			{
				if(n == 200000 and x == 149 and i >= 200)
				{
					//cout << "if" << endl;
				}
				if(best[x - len] != 2000000100 and (ans == -1 or ans > one * current.cost + one * best[x - len]))
				{
					if(n == 200000 and x == 149 and i >= 200)
					{
						//cout << "better" << endl;
					}
					ans = one * current.cost + one * best[x - len];
				}
			}
			if(n == 200000 and x == 149 and i >= 200)
			{
				//cout << "end" << endl;
			}
		}

		else
		{
			//if(n == 200000 and x == 149 and i >= 200)
			//	cout << "right " << i << endl;
			//cout << len << " " << best[len] << " " << current.cost << endl;
			if(/*best[len] == 0 or */best[len] > current.cost)
			{
				best[len] = current.cost;
			}
		}
	}

	//cout << best[2] << " " << best[3] << endl;

	cout << ans << endl;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<set>
#include<ctime>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;
const int N=200001,COST=2000000001;

struct voucher{
	int l;
	int r;
	int c;
}p1[N],p2[N];

bool cmp1(voucher a,voucher b){
	return a.l<b.l;
}

bool cmp2(voucher a,voucher b){
	return a.r<b.r;
}

int main(){
	int n,x;
	cin>>n>>x;
	int ans=COST;
	int duration[N];
	for(int i=0;i<n;i++){
		cin>>p1[i].l>>p1[i].r>>p1[i].c;
		p2[i]=p1[i];
	}
	sort(p1,p1+n,cmp1);
	sort(p2,p2+n,cmp2);
	int j=0;
	for(int i=0;i<n;i++){
		for(;p2[j].r<p1[i].l&&j<n;j++){
			int temp=p2[j].r-p2[j].l+1;
			if(duration[temp]){
				duration[temp]=min(duration[temp],p2[j].c);
			}
			else{
				duration[temp]=p2[j].c;
			}
		}
		int temp=x-(p1[i].r-p1[i].l+1);
		if(temp>0&&duration[temp]){
			ans=min(ans,duration[temp]+p1[i].c);
		}
	}
	if(ans<COST)cout<<ans;
	else cout<<"-1";
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

struct voucher{
	int l;
	int r;
	int cost;
};

bool cmp1(voucher a,voucher b){
	return a.l<=b.l?1:0;
}

bool cmp2(voucher a,voucher b){
	return a.r<=b.r?1:0;
}

int main()
{
	int n,x;
	int minn=INT_MAX;
	bool has=0;
	cin>>n>>x;
	voucher *p1=new voucher[n];
	voucher *p2=new voucher[n];
	int *duration=new int[n];
	for(int i=0;i<n;i++){
		cin>>p1[i].l>>p1[i].r>>p1[i].cost;
		p2[i].l=p1[i].l;
		p2[i].r=p1[i].r;
		p2[i].cost=p1[i].cost;
	}
	sort(p1,p1+n,cmp1);
	sort(p2,p2+n,cmp2);
	int j=0;
	for(int i=0;i<n;i++){
		for(;j<n&&p2[j].r<p1[i].l;j++){
			if(duration[p2[j].r-p2[j].l+1])
				duration[p2[j].r-p2[j].l+1]=min(duration[p2[j].r-p2[j].l+1],p2[j].cost);
			else
				duration[p2[j].r-p2[j].l+1]=p2[j].cost;
		}
		int k=x-(p1[i].r-p1[i].l+1);
		if(k>0&&duration[k]&&minn>duration[k]+p1[i].cost)
			minn=duration[k]+p1[i].cost;
	}
	if(minn==INT_MAX)cout<<-1;
	else cout<<minn;
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<set>
#include<ctime>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;
inline void write(long long a){
	if(a>=10)write(a/10);
	putchar('0'+a%10);
}

inline void writeln(int a){
	write(a); puts("");
}

struct voucher{
	int l;
	int r;
	int cost;
};

bool cmp1(voucher a,voucher b){
	return a.l<=b.l?1:0;
}

bool cmp2(voucher a,voucher b){
	return a.r<=b.r?1:0;
}

int main()
{
	int n,x;
	int minn=INT_MAX;
	bool has=0;
	cin>>n>>x;
	voucher *p1=new voucher[n];
	voucher *p2=new voucher[n];
	int *duration=new int[n];
	for(int i=0;i<n;i++){
		cin>>p1[i].l>>p1[i].r>>p1[i].cost;
		p2[i].l=p1[i].l;
		p2[i].r=p1[i].r;
		p2[i].cost=p1[i].cost;
	}
	sort(p1,p1+n,cmp1);
	sort(p2,p2+n,cmp2);
	int j=0;
	for(int i=0;i<n;i++){
		for(;j<n&&p2[j].r<p1[i].l;j++){
			if(duration[p2[j].r-p2[j].l+1]){
				duration[p2[j].r-p2[j].l+1]=min(duration[p2[j].r-p2[j].l+1],p2[j].cost);
			}
			else{
				duration[p2[j].r-p2[j].l+1]=p2[j].cost;
			}
		}
		int k=x-(p1[i].r-p1[i].l+1);
		if(k>0&&duration[k])
			minn=min(minn,duration[k]+p1[i].cost);
	}

	if(minn<INT_MAX)
		writeln(minn);
	else
		puts("-1");
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

struct voucher{
	int l;
	int r;
	int cost;
};

bool cmp1(voucher a,voucher b){
	return a.l<=b.l?1:0;
}

bool cmp2(voucher a,voucher b){
	return a.r<=b.r?1:0;
}

int main()
{
	int n,x;
	int minn=INT_MAX;
	bool has=0;
	cin>>n>>x;
	voucher *p1=new voucher[n];
	voucher *p2=new voucher[n];
	int *duration=new int[n];
	for(int i=0;i<n;i++){
		cin>>p1[i].l>>p1[i].r>>p1[i].cost;
		p2[i].l=p1[i].l;
		p2[i].r=p1[i].r;
		p2[i].cost=p1[i].cost;
	}
	sort(p1,p1+n,cmp1);
	sort(p2,p2+n,cmp2);
	int j=0;
	for(int i=0;i<n;i++){
		for(;j<n&&p2[j].r<p1[i].l;j++){
			if(duration[p2[j].r-p2[j].l+1])
				duration[p2[j].r-p2[j].l+1]=min(duration[p2[j].r-p2[j].l+1],p2[j].cost);
			else
				duration[p2[j].r-p2[j].l+1]=duration[p2[j].cost];
		}
		int k=x-(p1[i].r-p1[i].l+1);
		if(k>0&&duration[k]&&minn>duration[k]+p1[i].cost)
			minn=duration[k]+p1[i].cost;
		cout<<minn;
	}
	if(minn==INT_MAX)cout<<-1;
	else cout<<minn;
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

struct voucher{
	int l;
	int r;
	int cost;
};

bool cmp1(voucher a,voucher b){
	return a.l<=b.l?1:0;
}

bool cmp2(voucher a,voucher b){
	return a.r<=b.r?1:0;
}

int main()
{
	int n,x;
	int minn=INT_MAX;
	bool has=0;
	cin>>n>>x;
	voucher *p1=new voucher[n];
	voucher *p2=new voucher[n];
	int *duration=new int[n];
	for(int i=0;i<n;i++){
		cin>>p1[i].l>>p1[i].r>>p1[i].cost;
		p2[i].l=p1[i].l;
		p2[i].r=p1[i].r;
		p2[i].cost=p1[i].cost;
	}
	sort(p1,p1+n,cmp1);
	sort(p2,p2+n,cmp2);
	int j=0;
	for(int i=0;i<n;i++){
		for(;j<n&&p2[j].r<p1[i].l;j++){
			if(duration[p2[j].r-p2[j].l+1]){
				duration[p2[j].r-p2[j].l+1]=min(duration[p2[j].r-p2[j].l+1],p2[j].cost);
			}
			else{
				duration[p2[j].r-p2[j].l+1]=p2[j].cost;
			}
		}
		int k=x-(p1[i].r-p1[i].l+1);
		if(k>0&&duration[k]&&minn>duration[k]+p1[i].cost)
			minn=duration[k]+p1[i].cost;
	}
	if(minn==INT_MAX)cout<<-1;
	else cout<<minn;
	return 0;
}


##include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<set>
#include<ctime>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;
inline void write(long long a){
	if(a>=10)write(a/10);
	putchar('0'+a%10);
}
inline void writeln(int a){
	write(a); puts("");
}
inline int read()//不管是正负数，都可以
{
	int x = 0; char ch = getchar(); bool positive = 1;
	for (; !isdigit(ch); ch = getchar())	if (ch == '-')	positive = 0;
	for (; isdigit(ch); ch = getchar())	x = x * 10 + ch - '0';
	return positive ? x : -x;
}
const int N=200005,oo=2000000005;
struct data{
	int l,r,cost;
}a[N],b[N];
int n,m,ans,tong[N],j;
inline bool cmp1(data a,data b){
	return a.r<b.r;
}
inline bool cmp2(data a,data b){
	return a.l<b.l;
}
int main(){
	n=read(); m=read();
	for(int i=1;i<=n;i++){a[i].l=read(); a[i].r=read(); a[i].cost=read();b[i]=a[i];}
	sort(&a[1],&a[n+1],cmp1); ans=oo;
	sort(&b[1],&b[n+1],cmp2); j=1;
	for(int i=1;i<=n;i++){
		for(;a[j].r<b[i].l;j++)if(!tong[a[j].r-a[j].l+1])tong[a[j].r-a[j].l+1]=a[j].cost; else tong[a[j].r-a[j].l+1]=min(tong[a[j].r-a[j].l+1],a[j].cost);
		if(m>=b[i].r-b[i].l+1&&tong[m-(b[i].r-b[i].l+1)])ans=min(ans,b[i].cost+tong[m-(b[i].r-b[i].l+1)]);
	}
	if(ans<oo)
	writeln(ans); else puts("-1");
}


#include <iostream>
#include <algorithm>
using namespace std;

struct voucher{
	int l;
	int r;
	int cost;
};

bool cmp1(voucher a,voucher b){
	return a.l<=b.l?1:0;
}

bool cmp2(voucher a,voucher b){
	return a.r<=b.r?1:0;
}

int main()
{
	int n,x;
	int minn=INT_MAX;
	bool has=0;
	cin>>n>>x;
	voucher *p1=new voucher[n];
	voucher *p2=new voucher[n];
	int *duration=new int[n];
	for(int i=0;i<n;i++){
		cin>>p1[i].l>>p1[i].r>>p1[i].cost;
		p2[i].l=p1[i].l;
		p2[i].r=p1[i].r;
		p2[i].cost=p1[i].cost;
	}
	sort(p1,p1+n,cmp1);
	sort(p2,p2+n,cmp2);
	int j=0;
	for(int i=0;i<n;i++){
		for(;j<n&&p2[j].r<p1[i].l;j++){
			if(duration[p2[j].r-p2[j].l+1]){
				duration[p2[j].r-p2[j].l+1]=min(duration[p2[j].r-p2[j].l+1],p2[j].cost);
			}
			else{
				duration[p2[j].r-p2[j].l+1]=p2[j].cost;
			}
		}
		int k=x-(p1[i].r-p1[i].l+1);
		if(k>0&&duration[k])
			minn=min(minn,duration[k]+p1[i].cost);
	}
	if(minn==INT_MAX)cout<<-1;
	else cout<<minn;
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

struct voucher{
	int l;
	int r;
	int cost;
};

bool cmp1(voucher a,voucher b){
	return a.l<=b.l?1:0;
}

bool cmp2(voucher a,voucher b){
	return a.r<=b.r?1:0;
}

int main()
{
	int n,x;
	int minn=INT_MAX;
	bool has=0;
	cin>>n>>x;
	voucher *p1=new voucher[n];
	voucher *p2=new voucher[n];
	int *duration=new int[n];
	for(int i=0;i<n;i++){
		cin>>p1[i].l>>p1[i].r>>p1[i].cost;
		p2[i].l=p1[i].l;
		p2[i].r=p1[i].r;
		p2[i].cost=p1[i].cost;
	}
	sort(p1,p1+n,cmp1);
	sort(p2,p2+n,cmp2);
	int j=0;
	for(int i=0;i<n;i++){
		for(;j<n&&p2[j].r<p1[i].l;j++){
			if(duration[p2[j].r-p2[j].l+1])
				duration[p2[j].r-p2[j].l+1]=min(duration[p2[j].r-p2[j].l+1],p2[j].cost);
			else
				duration[p2[j].r-p2[j].l+1]=duration[p2[j].cost];
		}
		int k=x-(p1[i].r-p1[i].l+1);
		if(k>0&&minn>duration[k]+p1[i].cost)
			minn=duration[k]+p1[i].cost;
	}
	if(minn==INT_MAX)cout<<-1;
	else cout<<minn;
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<set>
#include<ctime>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;
const int N=200000,COST=2000000000;

struct voucher{
	int l;
	int r;
	int c;
}p1[N],p2[N];

bool cmp1(voucher a,voucher b){
	return a.l<b.l;
}

bool cmp2(voucher a,voucher b){
	return a.r<b.r;
}

int main(){
	int n,x;
	cin>>n>>x;
	int ans=COST;
	int duration[N];
	for(int i=0;i<n;i++){
		cin>>p1[i].l>>p1[i].r>>p1[i].c;
		p2[i]=p1[i];
	}
	sort(p1,p1+n,cmp1);
	sort(p2,p2+n,cmp2);
	int j=0;
	for(int i=0;i<n;i++){
		for(;p2[j].r<p1[i].l&&j<n;j++){
			int temp=p2[j].r-p2[j].l+1;
			if(duration[temp]){
				duration[temp]=min(duration[temp],p2[j].c);
			}
			else{
				duration[temp]=p2[j].c;
			}
		}
		int temp=x-(p1[i].r-p1[i].l+1);
		if(temp>0&&duration[temp]){
			ans=min(ans,duration[temp]+p1[i].c);
		}
	}
	if(ans<COST)cout<<ans;
	else cout<<"-1";
	return 0;
}


#include<bits/stdc++.h>
#define LL int
#define s(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define ss(a) scanf("%s",a)
#define w(t) while(t--)
#define f(i,n) for(i=0;i<n;i++)
#define fd(i,n) for(i=n-1;i>=0;i--)
#define p(a) printf("%d",a)
#define pl(a) printf("%lld",a)
#define ps(a) printf("%s",a)
#define pc(a) printf("%c",a)
#define ent printf("\n")
#define mod 1000000007
#define PI 3.14159265
#define gs getline(cin,s)
#define pb push_back
#define mp make_pair
#define INF 1e18
#define pii pair<LL,LL>
using namespace std;

vector<pair<pii,pii> > v[200005];
vector<pair<int,pii> > v1;
//set<LL> st;

int main()
{

    std:ios_base::sync_with_stdio(false);

    LL n,x;
    cin>>n>>x;

    int i,j;
    LL a,b,c;

    f(i,n)
    {
        cin>>a>>b>>c;
        v1.pb(mp(c,mp(a,b)));
        v[b-a+1].pb(mp(mp(c,i),mp(a,b)));
  //      st.insert(b-a+1);
    }

  //  set<LL>:: iterator it;

       for(int k=1;k<=200000;k++)

        sort(v[k].begin(),v[k].end());

    LL mn=INT_MAX;
    for(i=0;i<v1.size();i++)
    {
        c=v1[i].first;
        a=v1[i].second.first;
        b=v1[i].second.second;
        LL k=b-a+1;

        if(x-k>0 && v[x-k].size())
        {
            for(j=0;j<v[x-k].size();j++)
            {
                LL cst=v[x-k][j].first.first;
                LL pos=v[x-k][j].first.second;
                LL a1=v[x-k][j].second.first;
                LL b1=v[x-k][j].second.second;
                if(b1<a || b<a1)
                {
                    if(mn>cst+c && i!=pos)
                    {
                        mn=cst+c;
                        break;
                    }
                }
            }
        }
    }

if(mn==1e12)
{
    cout<<-1<<endl;
    return 0;
}
    cout<<mn<<endl;

    return 0;

}