import java.lang.ref.SoftReference;
import java.util.*;
/**
* Til the Cows Come Home Dijkstra
*/
public class Main {
static List<int[]>[] adjList;
static int[] visited;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int T = scanner.nextInt();
int N = scanner.nextInt();
visited = new int[N+1];
adjList = new ArrayList[N+1];
for (int i=0;i<N;i++){
List<int[]> list = new ArrayList<int[]>();
adjList[i+1] = list;
visited[i+1] = Integer.MAX_VALUE;
}
for (int i=0;i<T;i++){
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = scanner.nextInt();
int[] p1 = new int[2];
p1[0] = b;
p1[1] = c;
int[] p2 = new int[2];
p2[0] = a;
p2[1] = c;
adjList[a].add(p1);
adjList[b].add(p2);
}
dij(1,N);
System.out.println(visited[N]);
}
public static void dij(int start,int end){
Queue<int[]> PQ = new PriorityQueue<int[]>(1,new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return o1[1] - o2[1];
}
});
visited[start] = 0;
int[] s = new int[2];
s[0] = start;s[1] = 0;
PQ.add(s);
while(!PQ.isEmpty()){
int[] curr = PQ.poll();
if (curr[0] == end){
return;
}
for (int i=0;i<adjList[curr[0]].size();i++){
int[] next = adjList[curr[0]].get(i).clone();
next[1] += curr[1];
if (next[1] < visited[next[0]]){
visited[next[0]] = next[1];
PQ.add(next);
}
}
}
}
}
import java.lang.ref.SoftReference;import java.util.*;/** * Til the Cows Come Home Dijkstra */public class Main { static List<int[]>[] adjList; static int[] visited; public static void main(String[] args) { Scanner scann.