import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] first = br.readLine().split(" ", 5); int n = Integer.parseInt(first[0]); int m = Integer.parseInt(first[1]); int k = Integer.parseInt(first[2]); int s = Integer.parseInt(first[3]); int t = Integer.parseInt(first[4]); ArrayList> graph = new ArrayList<>(); for (int i = 0; i < n - 1; i++) { graph.add(new ArrayList<>()); } for (int i = 0; i < m; i++) { String[] line = br.readLine().split(" ", 3); int a = Integer.parseInt(line[0]) - 1; int b = Integer.parseInt(line[1]); int c = Integer.parseInt(line[2]); graph.get(a).add(new Route(b, c)); } ArrayList> costs = new ArrayList<>(); for (int i = 0; i < n; i++) { costs.add(new HashMap<>()); } PriorityQueue queue = new PriorityQueue<>(); queue.add(new Path(0, s, 0)); while (queue.size() > 0) { Path p = queue.poll(); if (costs.get(p.idx).containsKey(p.floor)) { continue; } costs.get(p.idx).put(p.floor, p.value); if (p.idx == n - 1) { continue; } for (Route x : graph.get(p.idx)) { if (!costs.get(p.idx + 1).containsKey(x.end)) { queue.add(new Path(p.idx + 1, x.end, p.value + Math.abs(p.floor - x.start))); } } } long min = Long.MAX_VALUE; for (Map.Entry entry : costs.get(n - 1).entrySet()) { int key = entry.getKey(); long value = entry.getValue(); min = Math.min(min, Math.abs(key - t) + value); } if (min == Long.MAX_VALUE) { System.out.println(-1); } else { System.out.println(min); } } 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; } } } static class Route { int start; int end; public Route(int start, int end) { this.start = start; this.end = end; } } }