# P,Qの少なくとも一方に対して往復してもTを越える場合は-1 # P,Q両方に2人とも行って帰ってこれればT # そうでなければ、どこで別れてどこで合流するかを全探索 import sys readline = sys.stdin.readline N,M,P,Q,T = map(int,readline().split()) P -= 1 Q -= 1 G = [[] for i in range(N)] for _ in range(M): a,b,c = map(int,readline().split()) G[a - 1].append([b - 1, c]) G[b - 1].append([a - 1, c]) import heapq as hq INF = 1 << 60 def get_dist(X): res = [INF] * N q = [] hq.heappush(q, (0, X)) while q: d, v = hq.heappop(q) if res[v] != INF: continue res[v] = d for child, c in G[v]: if res[child] != INF: continue hq.heappush(q, (d + c, child)) return res dist_from_0 = get_dist(0) dist_from_P = get_dist(P) dist_from_Q = get_dist(Q) # そもそもかえってこれない if max(dist_from_0[P], dist_from_0[Q]) * 2 > T: print(-1) exit(0) # 両方行ける if dist_from_0[P] + dist_from_P[Q] + dist_from_Q[0] <= T: print(T) exit(0) # そうでない場合、どこかで別れてどこかで合流する。全探索 ans = 0 # 少なくとも別々に行って帰ってこれるので、0は達成できる for i in range(N): for j in range(i, N): P_time = dist_from_P[i] + dist_from_P[j] # Pに寄る人が経由する時間 Q_time = dist_from_Q[i] + dist_from_Q[j] # Q # これらのうち大きい方が、一緒にいられない時間 leave = max(P_time, Q_time) # この二点を経由して帰ってこれるか? if dist_from_0[i] + dist_from_0[j] + leave > T: continue ans = max(ans, T - leave) print(ans)