{"trustable":false,"prependHtml":"\u003cstyle type\u003d\"text/css\"\u003e\n section pre {\n display: block;\n padding: 9.5px;\n margin: 0 0 10px;\n font-size: 13px;\n line-height: 1.42857143;\n word-break: break-all;\n word-wrap: break-word;\n color: #333;\n background: rgba(255, 255, 255, 0.5);\n border: 1px solid #ccc;\n border-radius: 6px;\n }\n\u003c/style\u003e\n\u003cscript\u003e\n window.katexOptions \u003d {\n delimiters: [\n {left: \u0027\\\\(\u0027, right: \u0027\\\\)\u0027, display: false},\n {left: \u0027\\\\[\u0027, right: \u0027\\\\]\u0027, display: true}\n ]\n };\n\u003c/script\u003e\n","sections":[{"title":"Problem Statement","value":{"format":"HTML","content":"\u003ch3\u003e\u003c/h3\u003e\n\u003cp\u003ePaul has \u003cvar\u003e\\(N\\)\u003c/var\u003e points arranged in a line: \u003cvar\u003e\\(1, ..., N\\)\u003c/var\u003e.\u003c/p\u003e \n\u003cp\u003eUsing these points as the vertices, Paul decides to make an undirected graph. First, the graph has no edge. Then, Paul will do \u003cvar\u003e\\(M\\)\u003c/var\u003e operations to add edges in this graph. At step \u003cvar\u003e\\(i\\)\u003c/var\u003e he will do as follows:\u003c/p\u003e \n\u003cul\u003e \n \u003cli\u003e given \u003cvar\u003e\\(L_i\\)\u003c/var\u003e and \u003cvar\u003e\\(R_i\\)\u003c/var\u003e between \u003cvar\u003e\\(1\\)\u003c/var\u003e and \u003cvar\u003e\\(N\\)\u003c/var\u003e (inclusive), and a positive integer \u003cvar\u003e\\(C_i\\)\u003c/var\u003e, for every pair of integers \u003cvar\u003e\\((s, t)\\)\u003c/var\u003e such that \u003cvar\u003e\\(L_i \\leq s \u0026lt; t \\leq R_i\\)\u003c/var\u003e, add an edge of length \u003cvar\u003e\\(C_i\\)\u003c/var\u003e between Vertex \u003cvar\u003e\\(s\\)\u003c/var\u003e and Vertex \u003cvar\u003e\\(t\\)\u003c/var\u003e.\u003c/li\u003e \n\u003c/ul\u003e \n\u003cp\u003eThe integers \u003cvar\u003e\\(L_1, ..., L_M\\)\u003c/var\u003e, \u003cvar\u003e\\(R_1, ..., R_M\\)\u003c/var\u003e, \u003cvar\u003e\\(C_1, ..., C_M\\)\u003c/var\u003e are all given as input.\u003c/p\u003e \n\u003cp\u003ePaul wants to solve the shortest path problem in the final graph obtained. Find the length of the shortest path from Vertex \u003cvar\u003e\\(1\\)\u003c/var\u003e to Vertex \u003cvar\u003e\\(N\\)\u003c/var\u003e in the final graph.\u003c/p\u003e"}},{"title":"Constraints","value":{"format":"HTML","content":"\u003ch3\u003e\u003c/h3\u003e\n\u003cul\u003e \n \u003cli\u003e\u003cvar\u003e\\(2 \\leq N \\leq 10^5\\)\u003c/var\u003e\u003c/li\u003e \n \u003cli\u003e\u003cvar\u003e\\(1 \\leq M \\leq 10^5\\)\u003c/var\u003e\u003c/li\u003e \n \u003cli\u003e\u003cvar\u003e\\(1 \\leq L_i \u0026lt; R_i \\leq N\\)\u003c/var\u003e\u003c/li\u003e \n \u003cli\u003e\u003cvar\u003e\\(1 \\leq C_i \\leq 10^9\\)\u003c/var\u003e\u003c/li\u003e \n\u003c/ul\u003e"}},{"title":"Input","value":{"format":"HTML","content":"\u003ch3\u003e\u003c/h3\u003e\n\u003cp\u003eInput is given from Standard Input in the following format:\u003c/p\u003e \n\u003cpre\u003e\u003cvar\u003e\\(N\\)\u003c/var\u003e \u003cvar\u003e\\(M\\)\u003c/var\u003e\n\u003cvar\u003e\\(L_1\\)\u003c/var\u003e \u003cvar\u003e\\(R_1\\)\u003c/var\u003e \u003cvar\u003e\\(C_1\\)\u003c/var\u003e\n\u003cvar\u003e\\(:\\)\u003c/var\u003e\n\u003cvar\u003e\\(L_M\\)\u003c/var\u003e \u003cvar\u003e\\(R_M\\)\u003c/var\u003e \u003cvar\u003e\\(C_M\\)\u003c/var\u003e\n\u003c/pre\u003e"}},{"title":"Output","value":{"format":"HTML","content":"\u003ch3\u003e\u003c/h3\u003e\n\u003cp\u003ePrint the length of the shortest path from Vertex \u003cvar\u003e\\(1\\)\u003c/var\u003e to Vertex \u003cvar\u003e\\(N\\)\u003c/var\u003e in the final graph. If there is no shortest path, print \u003ccode\u003e-1\u003c/code\u003e instead.\u003c/p\u003e"}},{"title":"Sample Input 1","value":{"format":"HTML","content":"\u003ch3\u003e\u003c/h3\u003e\n\u003cpre\u003e4 3\n1 3 2\n2 4 3\n1 4 6\n\u003c/pre\u003e"}},{"title":"Sample Output 1","value":{"format":"HTML","content":"\u003ch3\u003e\u003c/h3\u003e\n\u003cpre\u003e5\n\u003c/pre\u003e \n\u003cp\u003eWe have an edge of length \u003cvar\u003e\\(2\\)\u003c/var\u003e between Vertex \u003cvar\u003e\\(1\\)\u003c/var\u003e and Vertex \u003cvar\u003e\\(2\\)\u003c/var\u003e, and an edge of length \u003cvar\u003e\\(3\\)\u003c/var\u003e between Vertex \u003cvar\u003e\\(2\\)\u003c/var\u003e and Vertex \u003cvar\u003e\\(4\\)\u003c/var\u003e, so there is a path of length \u003cvar\u003e\\(5\\)\u003c/var\u003e between Vertex \u003cvar\u003e\\(1\\)\u003c/var\u003e and Vertex \u003cvar\u003e\\(4\\)\u003c/var\u003e.\u003c/p\u003e"}},{"title":"Sample Input 2","value":{"format":"HTML","content":"\u003ch3\u003e\u003c/h3\u003e\n\u003cpre\u003e4 2\n1 2 1\n3 4 2\n\u003c/pre\u003e"}},{"title":"Sample Output 2","value":{"format":"HTML","content":"\u003ch3\u003e\u003c/h3\u003e\n\u003cpre\u003e-1\n\u003c/pre\u003e"}},{"title":"Sample Input 3","value":{"format":"HTML","content":"\u003ch3\u003e\u003c/h3\u003e\n\u003cpre\u003e10 7\n1 5 18\n3 4 8\n1 3 5\n4 7 10\n5 9 8\n6 10 5\n8 10 3\n\u003c/pre\u003e"}},{"title":"Sample Output 3","value":{"format":"HTML","content":"\u003ch3\u003e\u003c/h3\u003e\n\u003cpre\u003e28\n\u003c/pre\u003e"}}]}