1 const maxm=20000+1000;
2 var a,b,c,d,fa:array[0..maxm] of longint;
3 i,n,m,k,l,r,mid:longint;
4 procedure swap(var x,y:longint);
5 var t:longint;
6 begin
7 t:=x;x:=y;y:=t;
8 end;
9 procedure sort(l,r:longint);
10 var i,j,x,y:longint;
11 begin
12 i:=l;j:=r;x:=c[(i+j)>>1];
13 repeat
14 while c[i]<x do inc(i);
15 while c[j]>x do dec(j);
16 if i<=j then
17 begin
18 swap(a[i],a[j]);swap(b[i],b[j]);
19 swap(c[i],c[j]);swap(d[i],d[j]);
20 inc(i);dec(j);
21 end;
22 until i>j;
23 if i<r then sort(i,r);
24 if j>l then sort(l,j);
25 end;
26 procedure init;
27 begin
28 readln(n,k,m);dec(m);
29 for i:=1 to m do readln(a[i],b[i],c[i],d[i]);
30 end;
31 function find(x:longint):longint;
32 begin
33 if fa[x]<>x then fa[x]:=find(fa[x]);
34 exit(fa[x]);
35 end;
36
37 function test(x:longint):boolean;
38 var i,j,cnt,tot:longint;
39 begin
40 for i:=1 to n do fa[i]:=i;cnt:=0;tot:=0;
41 j:=1;
42 while (j<=m) do
43 begin
44 while (find(a[j])=find(b[j])) and (j<m) do inc(j);
45 if find(a[j])=find(b[j]) then break;
46 if (c[j]<=x) or ((c[j]>x) and (d[j]<=x) and (tot>=k)) then
47 begin
48 inc(cnt);fa[find(a[j])]:=find(b[j]);
49 if c[j]<=x then inc(tot);
50 end;
51 inc(j);
52 end;
53 exit(cnt=n-1);
54 end;
55 procedure main;
56 begin
57 sort(1,m);
58 l:=0;r:=30000;
59 while l<r do
60 begin
61 mid:=(l+r)>>1;
62 if test(mid) then r:=mid else l:=mid+1;
63 end;
64 writeln(l);
65 end;
66
67 begin
68 assign(input,‘input.txt‘);assign(output,‘output.txt‘);
69 reset(input);rewrite(output);
70 init;
71 main;
72 close(input);close(output);
73 end.