package bj.gold.l5;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class BJ_G5_1446_지름길2 {
static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
static int N;
static int D;
static List<ShortPath> paths;
static Pair[] pairs;
static int INF = 987654321;
public static void main(String[] args) throws IOException {
input = new BufferedReader(new StringReader(src));
StringTokenizer tokens = new StringTokenizer(input.readLine());
N = Integer.parseInt(tokens.nextToken());
D = Integer.parseInt(tokens.nextToken());
paths = new ArrayList<>();
for (int i = 0; i < N; i++) {
tokens = new StringTokenizer(input.readLine());
int from = Integer.parseInt(tokens.nextToken());
int to = Integer.parseInt(tokens.nextToken());
int dist = Integer.parseInt(tokens.nextToken());
// 역주행은 할 수 없다.: D 이내여야 가능
if (to > D) {
continue;
}
// 지름길은 지름길이어야지..
if (dist >= (to - from)) {
continue;
}
paths.add(new ShortPath(from, to, dist));
}
// 탐색 시작
pairs = new Pair[D + 1];
PriorityQueue<Pair> pq = new PriorityQueue<>();
for (int i = 0; i < pairs.length; i++) {
Pair p = new Pair(i, INF);
pairs[i] = p;
if (i == 0) {
p.dist = 0;
pq.offer(p);
}
}
// 출발 지점 초기화
while (!pq.isEmpty()) {
Pair front = pq.poll();
if (front.no == D) {
break;
}
// 다음 자식은: 1칸 가거나 지름길 있으면 그걸로 가거나.
Pair next = pairs[front.no + 1];
if (next.dist > front.dist + 1) {
next.dist = front.dist + 1;
pq.offer(next);
}
// 지름길 있으면 그쪽으로 가봐?
for (ShortPath path : paths) {
if (path.from == front.no) {
Pair spNext = pairs[path.to];
if (spNext.dist > front.dist + path.dist) {
spNext.dist = front.dist + path.dist;
pq.offer(spNext);
}
}
}
}
System.out.println(pairs[D].dist);
}
static class Pair implements Comparable<Pair> {
int no, dist;
public Pair(int no, int dist) {
this.no = no;
this.dist = dist;
}
@Override
public int compareTo(Pair o) {
return Integer.compare(this.dist, o.dist);
}
}
static class ShortPath {
int from;
int to;
int dist;
public ShortPath(int from, int to, int dist) {
this.from = from;
this.to = to;
this.dist = dist;
}
}
private static String src = "5 150\r\n" +
"0 50 10\r\n" +
"0 50 20\r\n" + // 더 긴 지름길이 왠말이냐..
"50 100 10\r\n" +
"50 151 10\r\n" + // 역주행은 안되니까 151은 의미 없다.
"110 140 10"; // 이게 무슨 지름길이냐?
}