#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <queue>
using namespace std;
#define MAXN 111111
#define INF 0x1f1f1f1f
int head[MAXN], head1[MAXN], ecnt1, ecnt2;
int dist[MAXN];
bool vis[MAXN];
int sta, end;
struct Edge
{
int v, t, next;
Edge(){}
Edge( int _v, int _t)
{
v = _v;
t = _t;
}
}E[MAXN], E1[MAXN];
void init()
{
memset(head, -1, sizeof(head));
memset(head1, -1, sizeof(head1));
ecnt1 = ecnt2 = 0;
}
void AddEdge( int u, int v, int t)
{
E[ecnt1] = Edge(v,t);
E[ecnt1].next = head[u];
head[u] = ecnt1++;
E1[ecnt2] = Edge(u,t);
E1[ecnt2].next = head1[v];
head1[v] = ecnt2++;
}
struct point
{
int v, g;
point(){};
point(int _v, int _g)
{
v = _v;
g = _g;
}
bool operator <(const point &a) const
{
return dist[v] + g > dist[a.v] + a.g;
}
};
priority_queue<point> que;
void dij( int sour)
{
memset(dist, INF, sizeof(dist));
memset(vis, 0, sizeof(vis));
dist[sour] = 0;
while(!que.empty()) que.pop();
point now ;
now = point(sour,0);
que.push(now);
point tem;
while(!que.empty())
{
now = que.top();
que.pop();
//cout<<head1[now.v]<<"sfd"<<endl;
if(vis[now.v])
continue;
vis[now.v] = true;
for( int i = head1[now.v]; i != -1; i = E1[i].next)
{
int t = E1[i].v;
int cost = E1[i].t;
if(dist[t] > dist[now.v] + cost)
{
dist[t] = dist[now.v] + cost;
que.push(point(t,0));
}
}
}
}
int a_start( int k)
{
while(!que.empty()) que.pop();
point now, tem;
now = point(sta, 0);
que.push(now);
while(!que.empty())
{
now = que.top();
que.pop();
if(now.v == end)
{
if(k > 1)
k--;
else
return now.g;
}
for( int i = head[now.v]; i != -1; i = E[i].next)
{
que.push(point(E[i].v,E[i].t + now.g));
}
}
return -1;
}
int main()
{
int n, m, k;
while(scanf("%d %d",&n, &m) != EOF)
{
init();
int u, v, t;
for( int i = 0; i < m; i++ )
{
scanf("%d %d %d",&u, &v, &t);
AddEdge(u, v, t);
}
scanf("%d %d %d",&sta, &end, &k);
dij(end);
if(!dist[sta] == INF)
{
printf("-1\n");
continue;
}
if(sta == end)
k++;
printf("%d\n",a_start(k));
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <queue>
using namespace std;
#define MAXN 111111
#define INF 0x1f1f1f1f
int head[MAXN], head1[MAXN], ecnt1, ecnt2;
int dist[MAXN];
bool vis[MAXN];
int sta, end;
struct Edge
{
int v, t, next;
Edge(){}
Edge( int _v, int _t)
{
v = _v;
t = _t;
}
}E[MAXN], E1[MAXN];
void init()
{
memset(head, -1, sizeof(head));
memset(head1, -1, sizeof(head1));
ecnt1 = ecnt2 = 0;
}
void AddEdge( int u, int v, int t)
{
E[ecnt1] = Edge(v,t);
E[ecnt1].next = head[u];
head[u] = ecnt1++;
E1[ecnt2] = Edge(u,t);
E1[ecnt2].next = head1[v];
head1[v] = ecnt2++;
}
struct point
{
int v, g;
point(){};
point(int _v, int _g)
{
v = _v;
g = _g;
}
bool operator <(const point &a) const
{
return dist[v] + g > dist[a.v] + a.g;
}
};
priority_queue<point> que;
void dij( int sour)
{
memset(dist, INF, sizeof(dist));
memset(vis, 0, sizeof(vis));
dist[sour] = 0;
while(!que.empty()) que.pop();
point now ;
now = point(sour,0);
que.push(now);
point tem;
while(!que.empty())
{
now = que.top();
que.pop();
//cout<<head1[now.v]<<"sfd"<<endl;
if(vis[now.v])
continue;
vis[now.v] = true;
for( int i = head1[now.v]; i != -1; i = E1[i].next)
{
int t = E1[i].v;
int cost = E1[i].t;
if(dist[t] > dist[now.v] + cost)
{
dist[t] = dist[now.v] + cost;
que.push(point(t,0));
}
}
}
}
int a_start( int k)
{
while(!que.empty()) que.pop();
point now, tem;
now = point(sta, 0);
que.push(now);
while(!que.empty())
{
now = que.top();
que.pop();
if(now.v == end)
{
if(k > 1)
k--;
else
return now.g;
}
for( int i = head[now.v]; i != -1; i = E[i].next)
{
que.push(point(E[i].v,E[i].t + now.g));
}
}
return -1;
}
int main()
{
int n, m, k;
while(scanf("%d %d",&n, &m) != EOF)
{
init();
int u, v, t;
for( int i = 0; i < m; i++ )
{
scanf("%d %d %d",&u, &v, &t);
AddEdge(u, v, t);
}
scanf("%d %d %d",&sta, &end, &k);
dij(end);
if(!dist[sta] == INF)
{
printf("-1\n");
continue;
}
if(sta == end)
k++;
printf("%d\n",a_start(k));
}
return 0;
}