#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll read(){
ll x=0,f=1;
char c=getchar();
while(c<'0'||c>'9'){
if(c=='-') f=-1;
c=getchar();
}
while(c>='0'&&c<='9'){
x=(x<<3)+(x<<1)+c-'0';
c=getchar();
}
return x*f;
}
inline void write(ll x){
if(x<0){
x=-x;
putchar('-');
}
if(x>9) write(x/10);
putchar(x%10+'0');
}
const int N=1e6+1e3+5;
int n,p,k,d[N],vis[N],ans=0x7fffffff;
struct node{
int i,v;
friend bool operator <(const node a,const node b){
return a.v>b.v;
}
};
vector<node> v[N];
void spfa(){
priority_queue<node> q;
d[1]=0;
q.push({1,0});
vis[1]=1;
while(!q.empty()){
int u=q.top().i;
q.pop();
for(auto i:v[u]){
d[i.i]=min(d[i.i],max(d[u],i.v));
if(!vis[i.i]){
vis[i.i]=1;
q.push({i.i,d[i.i]});
}
}
}
for(int i=n;i<=n*k+n;i+=n){
ans=min(ans,d[i]);
}
}
int main(){
memset(d,0x3f,sizeof d);
n=read(),p=read(),k=read();
for(int i=1;i<=p;i++){
int a=read(),b=read(),l=read();
for(int j=0;j<k;j++){
v[a+j*n].push_back({b+j*n,l});
v[b+j*n].push_back({a+j*n,l});
v[a+j*n].push_back({b+j*n+n,0});
v[b+j*n].push_back({a+j*n+n,0});
}
}
spfa();
if(ans==0x3f3f3f3f){
write(-1);
return 0;
}
write(ans);
return 0;
}