1 const inf=maxlongint>>2;maxn=100000+10;
2 type node=record
3 from,go,next,v1,v2:longint;
4 end;
5 var a,b,d,c1,c2,head,fa:array[0..maxn] of longint;
6 q:array[0..100*maxn] of longint;
7 e:array[0..maxn*2] of node;
8 v:array[0..maxn] of boolean;
9 i,j,xx,yy,l,z,w,tot,r,x,y:longint;
10 ans,n,m,mm:int64;
11 function find(x:longint):longint;
12 begin
13 if fa[x]<>x then fa[x]:=find(fa[x]);
14 exit(fa[x]);
15 end;
16 function max(x,y:longint):longint;
17 begin
18 if x>y then exit(x) else exit(y);
19 end;
20 function min(x,y:longint):longint;
21 begin
22 if x<y then exit(x) else exit(y);
23 end;
24 procedure swap(var x,y:longint);
25 var t:longint;
26 begin
27 t:=x;x:=y;y:=t;
28 end;
29
30 procedure sort(l,r:longint);
31 var i,j,x:longint;
32 begin
33 i:=l;j:=r;x:=a[(i+j)>>1];
34 repeat
35 while a[i]<x do inc(i);
36 while a[j]>x do dec(j);
37 if i<=j then
38 begin
39 swap(a[i],a[j]);swap(b[i],b[j]);swap(c1[i],c1[j]);swap(c2[i],c2[j]);
40 inc(i);dec(j);
41 end;
42 until i>j;
43 if i<r then sort(i,r);
44 if j>l then sort(l,j);
45 end;
46 procedure insert(x,y,z,w:longint);
47 begin
48 inc(tot);
49 with e[tot] do
50 begin
51 from:=x;go:=y;next:=head[x];head[x]:=tot;v1:=z;v2:=w;
52 end;
53 end;
54 procedure spfa;
55 var i,x,y,h,t,tmp:longint;
56 begin
57 //writeln(1);
58 h:=0;t:=0;
59 for i:=1 to n do if v[i] then begin inc(t);q[t]:=i;end;
60 //for i:=1 to t do writeln(q[i]);
61 while h<t do
62 begin
63 inc(h);
64 x:=q[h];v[x]:=false; //writeln(x);
65 i:=head[x];
66 while i<>0 do
67 begin
68 y:=e[i].go;
69 tmp:=max(d[x],e[i].v2);
70 //writeln(e[i].v1,‘ ‘,xx,‘ ‘,tmp,‘ ‘,d[y]);
71 if (e[i].v1<=xx) and (tmp<d[y]) then
72 begin
73 d[y]:=tmp;//writeln(d[y]);
74 if not(v[y]) then
75 begin
76 v[y]:=true;
77 inc(t);q[t]:=y;
78 end;
79 end;
80 i:=e[i].next;
81 end;
82 end;
83 end;
84 procedure init;
85 begin
86 readln(n,m);mm:=m;m:=0;
87 for i:=1 to n do fa[i]:=i;
88 for i:=1 to mm do
89 begin
90 readln(x,y,z,w);
91 xx:=find(x);yy:=find(y);
92 if xx<>yy then fa[xx]:=yy;
93 if x=y then continue;
94 inc(m);c1[m]:=x;c2[m]:=y;a[m]:=z;b[m]:=w;
95 insert(x,y,z,w);insert(y,x,z,w);
96 end;
97 sort(1,m);//writeln(m);
98 //for i:=1 to m do writeln(i,‘ ‘,c1[i],‘ ‘,c2[i],‘ ‘,a[i],‘ ‘,b[i]);
99 end;
100 procedure main;
101 begin
102 d[1]:=0;
103 for i:=2 to n do d[i]:=inf;
104 //for i:=1 to n do writeln(i,‘ ‘,d[i],‘ ‘,head[i]);
105 l:=1;r:=m;
106 for i:=1 to n do fa[i]:=i;
107 while find(1)<>find(n) do
108 begin
109 //writeln(l,‘ ‘,find(1),‘ ‘,find(n));
110 //writeln(c1[l],‘ ‘,c2[l]);
111 xx:=find(c1[l]);yy:=find(c2[l]);
112 if xx<>yy then fa[xx]:=yy;
113 inc(l);
114 end;
115 //writeln(l,‘ ‘,find(1),‘ ‘,find(n));
116 while a[l]=a[l-1] do inc(l);//writeln(l);
117 v[1]:=true;xx:=a[l-1];//writeln(xx);
118 spfa;
119 //for i:=1 to n do writeln(i,‘ ‘,d[i]);
120 ans:=min(ans,d[n]+xx);//writeln(‘ ‘,ans);
121 while true do
122 begin
123 //writeln(‘ ‘,ans);
124 fillchar(v,sizeof(v),false);
125 if l>r then break;
126 while a[l]=a[l+1] do
127 begin
128 v[c1[l]]:=true;v[c2[l]]:=true;
129 inc(l);
130 end;
131 v[c1[l]]:=true;v[c2[l]]:=true;
132 xx:=a[l]; //writeln(xx);
133 if l>r then break;
134 spfa;
135 //for i:=1 to n do writeln(i,‘ ‘,d[i]);
136 //if d[n]+xx=19923 then writeln(c1[l],‘ ‘,c2[l],‘ ‘,a[l],‘ ‘,b[l]);
137 //writeln(d[n],‘ ‘,xx);
138 ans:=min(ans,d[n]+xx);//writeln(d[n],‘ ‘,l,‘ ‘,r,‘ ‘,xx);
139 while ans<=a[r] do dec(r);
140 inc(l);
141 end;
142 writeln(ans);
143 end;
144 begin
145 assign(input,‘forest.in‘);assign(output,‘forest.out‘);
146 reset(input);rewrite(output);
147 init;
148 ans:=inf<<1;
149 if find(1)<>find(n) then writeln(‘-1‘) else main;
150 close(input);close(output);
151 end.