import java.util.*; public class PFSPath { static ArrayList graph = new ArrayList<>(); public static void main(String[] args) { Scanner sc = new Scanner(System.in); // read from Standard Input int n = sc.nextInt(); // nubmer of nodes int m = sc.nextInt(); // nubmer of edges for (int i=0; i fixed = new HashSet<>(); // fixed node src.d = 0; // distance of start node is 0 var queue = new PriorityQueue(); queue.add(src); while (queue.size() > 0) { Node x = queue.poll(); // get minimum node from queue assert !fixed.contains(x); if (x == dst) return x.d; // find the answer fixed.add(x); // set node x as fixed for (Map.Entry entry: x.edges.entrySet()) { // each edge from node x Node y = graph.get(entry.getKey()); // edge's endpoint int w = entry.getValue(); // edge's weights int d2 = x.d + w; // distance to y via x if (fixed.contains(y) || d2 >= y.d) continue; // do nothing if fixed or further if (queue.contains(y)) queue.remove(y); // remove y from queue y.d = d2; // update distance to y y.via = x; // set the previous node of y is x queue.add(y); // add y into queue } } return Integer.MAX_VALUE; // unconnected } static ArrayList getRoute(Node src, Node dst) { // shortest path from src to dst ArrayList r = new ArrayList<>(); r.add(dst); // start from dst Node x = dst; while (x != src) { // trace back to src x = x.via; r.add(x); } Collections.reverse(r); // reverse the list (src->dst) return r; } static class Node implements Comparable { // node int id; // node identity int d; // distance Node via; // previous node HashMap edges; // edge: (edge's endpoint, weights) public Node(int id, int d) { this.id = id; this.d = d; edges = new HashMap<>(); } void addEdge(int t, int w) { edges.put(t, w); } public int compareTo(Node x) { return d - x.d; } } }