from collections import defaultdict import heapq INF = float('inf') def dijkstra(s, edge): dist = defaultdict(lambda: INF) dist[s] = 0 edgelist = [] heapq.heappush(edgelist,(dist[s], s)) while edgelist: minedge = heapq.heappop(edgelist) if dist[minedge[1]] < minedge[0]: continue v = minedge[1] for e in edge[v]: if dist[e[1]] > dist[v]+e[0]: dist[e[1]] = dist[v]+e[0] heapq.heappush(edgelist,(dist[e[1]], e[1])) return dist import sys import io, os input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline def main(): n,m,k,s,t = map(int, input().split()) s,t = s-1, t-1 edge = defaultdict(lambda: []) V = [[] for i in range(n)] V[0].append(0) V[n-1].append(t) for i in range(m): a, b, c = map(int, input().split()) a, b, c = a-1, b-1, c-1 edge[b*n+a].append((0, c*n+a+1)) V[a].append(b) V[a+1].append(c) for a, l in enumerate(V): l.sort() for i in range(len(l)-1): x, y = l[i], l[i+1] d = abs(x-y) edge[x*n+a].append((d, y*n+a)) edge[y*n+a].append((d, x*n+a)) t = t*n+n-1 D = dijkstra(s, edge) if D[t] < INF: print(D[t]) else: print(-1) if __name__ == '__main__': main()