import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); int m = sc.nextInt(); int k = sc.nextInt(); int s = sc.nextInt(); int t = sc.nextInt(); ArrayList> floors = new ArrayList<>(); ArrayList> dp = new ArrayList<>(); ArrayList>> graph = new ArrayList<>(); for (int i = 0; i < n; i++) { floors.add(new TreeSet<>()); dp.add(new HashMap<>()); graph.add(new HashMap<>()); } floors.get(0).add(s); floors.get(n - 1).add(t); for (int i = 0; i < m; i++) { int a = sc.nextInt() - 1; int b = sc.nextInt(); int c = sc.nextInt(); floors.get(a).add(b); floors.get(a + 1).add(c); if (!graph.get(a).containsKey(b)) { graph.get(a).put(b, new ArrayList<>()); } graph.get(a).get(b).add(c); } PriorityQueue queue = new PriorityQueue<>(); queue.add(new Path(0, s, 0)); while (queue.size() > 0) { Path p = queue.poll(); if (dp.get(p.idx).containsKey(p.floor)) { continue; } dp.get(p.idx).put(p.floor, p.value); if (floors.get(p.idx).first() != p.floor) { int next = floors.get(p.idx).lower(p.floor); queue.add(new Path(p.idx, next, p.value + p.floor - next)); } if (floors.get(p.idx).last() != p.floor) { int next = floors.get(p.idx).higher(p.floor); queue.add(new Path(p.idx, next, p.value + next - p.floor)); } if (graph.get(p.idx).containsKey(p.floor)) { for (int x : graph.get(p.idx).get(p.floor)) { queue.add(new Path(p.idx + 1, x, p.value)); } } } System.out.println(dp.get(n - 1).getOrDefault(t, -1L)); } static class Path implements Comparable { int idx; int floor; long value; public Path(int idx, int floor, long value) { this.idx = idx; this.floor = floor; this.value = value; } public int compareTo(Path another) { if (value == another.value) { return 0; } else if (value < another.value) { return -1; } else { return 1; } } } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public Scanner() throws Exception { } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public String next() throws Exception { if (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }