#include<cstdio>
#include<algorithm>
inline int read(){
int x=0,c=getchar();
while(c>‘9‘||c<‘0‘)c=getchar();
while(c>=‘0‘&&c<=‘9‘)x=x*10+c-‘0‘,c=getchar();
return x;
}
struct edge{
int a,b,c1,c2;
}es[20010];
bool operator<(edge a,edge b){
return a.c1<b.c1;
}
int n,k,m;
int f[10010];
inline int get(int x){
int a=x,c;
while(a!=f[a])a=f[a];
while(a!=(c=f[x]))f[x]=a,x=c;
return a;
}
bool chk(int x){
int ct=0,t=0;
for(int i=0;i<=n;i++)f[i]=i;
for(int i=0;i<m;i++){
if(es[i].c1<=x){
int x=get(es[i].a),y=get(es[i].b);
if(x!=y){
ct++;t++;
f[x]=y;
}
}else if(es[i].c2<=x){
int x=get(es[i].a),y=get(es[i].b);
if(x!=y){
t++;
f[x]=y;
}
}
}
return t>=n-1&&ct>=k;
}
int main(){
n=read();k=read();m=read();
m--;
for(int i=0;i<m;i++){
es[i].a=read();
es[i].b=read();
es[i].c1=read();
es[i].c2=read();
}
std::sort(es,es+m);
int l=0,r=30000,a;
while(l<r){
a=l+r>>1;
if(chk(a))r=a;
else l=a+1;
}
printf("%d",l);
return 0;
}