#!/usr/bin/env python3 # %% import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines from collections import defaultdict INF = 10 ** 18 # %% N, M, _, S, T = map(int, readline().split()) if M == 0: if N == 1: print(abs(S - T)) else: print(-1) exit() m = map(int, read().split()) A, B, C = zip(*zip(m, m, m)) # %% A_to_BC = [[] for _ in range(N + 1)] for a, b, c in zip(A, B, C): A_to_BC[a].append((b, c)) # %% A_to_BC[N].append((T, T)) # %% def to_next_building(a, before): event = [(b, c) for b, c in A_to_BC[a]] event += [(key, -value) for key, value in before.items() if value != INF] event += [(-INF, -INF), (INF, -INF)] event.sort() after = defaultdict(lambda: INF) min_const_term = INF for n, x in event: if x > 0: newcost = n + min_const_term if after[x] > newcost: after[x] = newcost else: const_term = -n - x if min_const_term > const_term: min_const_term = const_term min_const_term = INF for n, x in event[::-1]: if x > 0: newcost = -n + min_const_term if after[x] > newcost: after[x] = newcost else: const_term = n - x if min_const_term > const_term: min_const_term = const_term return after # %% dist = defaultdict(int) dist[S] = 0 for a in range(1, N + 1): dist = to_next_building(a, dist) answer = dist[T] if dist[T] < INF else -1 print(answer)