1 1 1 2
5
1000000000 1 10000
19920401 1188 5566
1000000000 1 10000
1 1 10000
5 100 200
 6 15 7 15
9
6 15 15
7 14 14
6 15 15
9 14 14
7 14 16
6 15 15
6 15 15
7 14 14
8 15 15
 1 1 1000000000 2
5
1000000000 1 10000
19920401 1188 5566
1000000000 1 10000
1 1 10000
5 100 200
 89 29 88 30
16
87 31 31
14 95 95
98 88 89
96 88 88
14 97 97
13 97 98
100 88 88
88 32 32
99 88 89
90 29 29
87 31 31
15 94 96
89 29 29
88 32 32
97 89 89
88 29 30
 30 14 39 19
31
35 7 11
37 11 12
32 13 13
37 5 6
46 13 13
37 14 14
31 13 13
43 13 19
45 15 19
46 13 13
32 17 17
41 14 19
30 14 14
43 13 17
34 16 18
44 11 19
38 13 13
40 12 20
37 16 18
46 16 18
34 10 14
36 9 10
36 15 19
38 15 19
42 13 19
33 14 15
35 15 19
33 17 18
39 12 20
36 5 7
45 12 12
 9 8 7 8
9
10 6 6
10 6 6
7 7 8
9 5 6
8 9 9
9 5 5
9 8 8
8 5 6
9 10 10
 2 1 1 1
2
1 1 2
2 1 2
 13 16 20 10
18
13 16 16
20 10 10
19 10 10
12 15 15
20 10 10
18 11 11
19 10 10
19 10 10
20 10 10
19 10 10
20 10 10
20 10 10
19 10 10
18 11 11
13 16 16
12 15 15
19 10 10
19 10 10

1
 1
 -1
 1
 9
 2
 1
 -1

import java.io.*;
import java.util.*;

public class C {

	void run() throws IOException {
		int m = 100000, h = 0, t = 0;
		int y_s = ni() - 1, x_s = ni() - 1, y_f = ni() - 1, x_f = ni() - 1;
		long inf = 1000000000;
		HashMap<Long, Integer> hm = new HashMap<Long, Integer>();
		int[] u = new int[m], d = new int[m], l = new int[m], r = new int[m];
		int[] ul = new int[m], ur = new int[m], dl = new int[m], dr = new int[m];
		for (int i = 0; i < m; i++)
			ul[i] = ur[i] = dl[i] = dr[i] = u[i] = d[i] = l[i] = r[i] = -1;

		int k = ni();
		m = 0;
		for (int i = 0, cur; i < k; i++) {
			long y = ni() - 1, a = ni() - 1, b = ni() - 1;
			for (long j = a; j <= b; j++) {
				long c = inf * y + j;
				long left = c - 1;
				long right = c + 1;
				long up = inf * (y + 1) + j;
				long down = inf * (y - 1) + j;

				long tul = up - 1;
				long tur = up + 1;
				long tdl = down - 1;
				long tdr = down + 1;

				hm.put(c, m);
				if (j > 0 && hm.containsKey(left)) {
					cur = hm.get(left);
					r[cur] = m;
					l[m] = cur;
				}
				if (j + 1 < inf && hm.containsKey(right)) {
					cur = hm.get(right);
					l[cur] = m;
					r[m] = cur;
				}
				if (y + 1 < inf && hm.containsKey(up)) {
					cur = hm.get(up);
					d[cur] = m;
					u[m] = cur;
				}
				if (y > 0 && hm.containsKey(down)) {
					cur = hm.get(down);
					u[cur] = m;
					d[m] = cur;
				}

				if (y + 1 < inf && j > 0 && hm.containsKey(tul)) {
					cur = hm.get(tul);
					dr[cur] = m;
					ul[m] = cur;
				}
				if (y + 1 < inf && j + 1 < inf && hm.containsKey(tur)) {
					cur = hm.get(tur);
					dl[cur] = m;
					ur[m] = cur;
				}

				if (y > 0 && j > 0 && hm.containsKey(tdl)) {
					cur = hm.get(tdl);
					ur[cur] = m;
					dl[m] = cur;
				}
				if (y > 0 && j + 1 < inf && hm.containsKey(tdr)) {
					cur = hm.get(tdr);
					ul[cur] = m;
					dr[m] = cur;
				}
				m++;
			}
		}
		int[] q = new int[m];
		long[] p = new long[m];
		Arrays.fill(p, inf);
		int finish = hm.get(x_f + y_f * inf);
		int start = hm.get(x_s + y_s * inf);
		p[start] = 0;
		q[h++] = start;
		while (h != t) {
			int i = q[t++];
			long v = p[i] + 1;
			if (u[i] != -1 && v < p[u[i]]) {
				p[u[i]] = v;
				q[h++] = u[i];
			}
			if (d[i] != -1 && v < p[d[i]]) {
				p[d[i]] = v;
				q[h++] = d[i];
			}

			if (r[i] != -1 && v < p[r[i]]) {
				p[r[i]] = v;
				q[h++] = r[i];
			}
			if (l[i] != -1 && v < p[l[i]]) {
				p[l[i]] = v;
				q[h++] = l[i];
			}
			if (ul[i] != -1 && v < p[ul[i]]) {
				p[ul[i]] = v;
				q[h++] = ul[i];
			}
			if (dl[i] != -1 && v < p[dl[i]]) {
				p[dl[i]] = v;
				q[h++] = dl[i];
			}

			if (dr[i] != -1 && v < p[dr[i]]) {
				p[dr[i]] = v;
				q[h++] = dr[i];
			}
			if (ur[i] != -1 && v < p[ur[i]]) {
				p[ur[i]] = v;
				q[h++] = ur[i];
			}

		}
		long z = p[finish];
		if (z == inf)
			z = -1;
		pw.print(z);

	}

	String next() throws IOException {
		while (st == null || !st.hasMoreTokens())
			st = new StringTokenizer(br.readLine());
		return st.nextToken();
	}

	int ni() throws IOException {
		return Integer.parseInt(next());
	}

	String nl() throws IOException {
		return br.readLine();
	}

	PrintWriter pw;
	BufferedReader br;
	StringTokenizer st;

	public static void main(String[] args) throws IOException {
		long timeout = System.currentTimeMillis();
		boolean CF = System.getProperty("ONLINE_JUDGE") != null;
		PrintWriter _pw = new PrintWriter(System.out);
		BufferedReader _br = new BufferedReader(CF ? new InputStreamReader(System.in) : new FileReader(new File("in.txt")));
		new C(_br, _pw).run();
		if (!CF) {
			_pw.println();
			_pw.println(System.currentTimeMillis() - timeout);
		}
		_br.close();
		_pw.close();
	}

	public C(BufferedReader _br, PrintWriter _pw) {
		br = _br;
		pw = _pw;
	}
}
