// Arup Guha
// 11/13/2017
// Solution to 2017 SER D1 Problem: Security Badges

import java.util.*;

public class security {

	public static int n;
	public static ArrayList[] g;
	public static int k;
	public static int start;
	public static int end;

	public static void main(String[] args) {

		// Form empty graph.
		Scanner stdin = new Scanner(System.in);
		n = stdin.nextInt();
		g = new ArrayList[n];
		for (int i=0; i<n; i++)
			g[i] = new ArrayList<edge>();
		int numE = stdin.nextInt();
		k = stdin.nextInt();

		// Get starting and ending locations.
		start = stdin.nextInt()-1;
		end = stdin.nextInt()-1;

		// Store critical values here.
		TreeSet<Integer> crit = new TreeSet<Integer>();;
		crit.add(1);
		crit.add(k+1);

		// Add edges.
		for (int i=0; i<numE; i++) {
			int a = stdin.nextInt()-1;
			int b = stdin.nextInt()-1;
			int min = stdin.nextInt();
			int max = stdin.nextInt();
			g[a].add(new edge(b,min,max));
			crit.add(min);
			crit.add(max+1);
		}

		// Store critical points in list form.
		ArrayList<Integer> list = new ArrayList<Integer>();
		while (crit.size() > 0 && crit.first().intValue() <= k+1) {
			list.add(crit.first());
			crit.pollFirst();
		}

		// Store result here.
		int res = 0;

		// Now, just test each critical range.
		for (int i=0; i<list.size()-1; i++)
			if (bfs(list.get(i)))
				res += (list.get(i+1) - list.get(i));

		// Ta da!
		System.out.println(res);
	}

	public static boolean bfs(int id) {

		// Set up BFS.
		LinkedList<Integer> q = new LinkedList<Integer>();
		q.offer(start);
		boolean[] used = new boolean[n];
		used[start] = true;

		// Run BFS.
		while (q.size() > 0) {

			// Get next.
			int cur = q.poll();
			if (cur == end) return true;

			// Try each door.
			for (edge e: (ArrayList<edge>)g[cur]) {

				// Don't go if we've been there before or we don't have clearance.
				if (used[e.v]) continue;
				if (id < e.min || id > e.max) continue;

				// Add to queue...
				used[e.v] = true;
				q.offer(e.v);
			}
		}

		// If we get here, we're good.
		return false;
	}
}

class edge {

	public int v;
	public int min;
	public int max;

	public edge(int to, int mymin, int mymax) {
		v = to;
		min = mymin;
		max = mymax;
	}
}