import numpy as np from scipy.sparse.csgraph import dijkstra from scipy.sparse import csr_matrix N, M, P, Q, T = map(int, input().split()) P -= 1 Q -= 1 edges = np.array([tuple(map(int, input().split())) for _ in range(M)]).T matr = csr_matrix((edges[2], (edges[:2] - 1)), shape=(N, N)) way_1, way_p, way_q = dijkstra(matr, indices=(0, P, Q), directed=False).astype(int) if way_1[P] + way_1[Q] + way_p[Q] <= T: print(T) exit() possible = ((way_1 + way_p) * 2 <= T) & ((way_1 + way_q) * 2 <= T) if not np.any(possible): print(-1) exit() time = T - np.maximum(way_p, way_q) * 2 print(time.max())