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

// practice with kaiboy
import java.io.*;
import java.util.*;

public class CF242C extends PrintWriter {
	CF242C() { super(System.out, true); }
	Scanner sc = new Scanner(System.in);
	public static void main(String[] $) {
		CF242C o = new CF242C(); o.main(); o.flush();
	}

	static class V {
		int x, l, r;
		V(int x, int l, int r) {
			this.x = x; this.l = l; this.r = r;
		}
	}
	V[] vv; int n;

	static class P {
		int x, y;
		P(int x, int y) {
			this.x = x; this.y = y;
		}
	}
	P[] pp; int k;

	int[] yy;
	void compress(int x, int i, int j) {
		for (int h = 0; h < j - i; h++) {
			V v = vv[i + h];
			yy[h << 1 | 0] = v.l << 1 | 0;
			yy[h << 1 | 1] = v.r << 1 | 1;
		}
		Arrays.sort(yy, 0, j - i << 1);
		for (int d = 0, l = -1, r, h = 0; h < j - i << 1; h++) {
			int y = yy[h];
			if ((y & 1) == 0) {
				if (d++ == 0)
					l = y >> 1;
			} else {
				if (--d == 0) {
					r = y >> 1;
					V v = vv[n++]; v.x = x; v.l = l; v.r = r;
				}
			}
		}
	}

	int find_vv(int x, int y) {
		int i, lower = -1, upper = n;
		while (upper - lower > 1) {
			i = (lower + upper) / 2;
			V v = vv[i];
			if (v.x < x || v.x == x && v.l <= y)
				lower = i;
			else
				upper = i;
		}
		if ((i = lower) != -1) {
			V v = vv[i];
			if (v.x == x && v.r > y)
				return i;
		}
		return -1;
	}

	int find_pp(int x, int y) {
		int h, lower = -1, upper = k;
		while (upper - lower > 1) {
			h = (lower + upper) / 2;
			P p = pp[h];
			if (p.x < x || p.x == x && p.y <= y)
				lower = h;
			else
				upper = h;
		}
		if ((h = lower) != -1) {
			P p = pp[h];
			if (p.x == x && p.y == y)
				return h;
		}
		return -1;
	}

	int[] eo; int[][] ej, ew;
	void init(int n) {
		eo = new int[n]; ej = new int[n][2]; ew = new int[n][2];
	}
	void append(int i, int j, int w) {
		int o = eo[i]++;
		if (o >= 2 && (o & o - 1) == 0) {
			ej[i] = Arrays.copyOf(ej[i], o << 1);
			ew[i] = Arrays.copyOf(ew[i], o << 1);
		}
		ej[i][o] = j;
		ew[i][o] = w;
	}

	static final long INF = 0x3f3f3f3f3f3f3f3fL;
	int[] pq, iq; int cnt;
	long[] dd;
	boolean lt(int i, int j) {
		return dd[i] < dd[j];
	}
	int p2(int p) {
		return (p *= 2) > cnt ? 0 : p < cnt && lt(iq[p + 1], iq[p]) ? p + 1 : p;
	}
	void pq_up(int i) {
		int j, p, q;
		for (p = pq[i]; (q = p / 2) > 0 && lt(i, j = iq[q]); p = q)
			iq[pq[j] = p] = j;
		iq[pq[i] = p] = i;
	}
	void pq_dn(int i) {
		int j, p, q;
		for (p = pq[i]; (q = p2(p)) > 0 && lt(j = iq[q], i); p = q)
			iq[pq[j] = p] = j;
		iq[pq[i] = p] = i;
	}
	void pq_add_last(int i) {
		iq[pq[i] = ++cnt] = i;
	}
	int pq_remove_first() {
		int i = iq[1], j = iq[cnt--];
		if (j != i) {
			pq[j] = 1; pq_dn(j);
		}
		pq[i] = 0;
		return i;
	}
	long dijkstra(int n, int s, int t) {
		pq = new int[n]; iq = new int[1 + n];
		dd = new long[n]; Arrays.fill(dd, INF);
		dd[s] = 0; pq_add_last(s);
		while (cnt > 0) {
			int i = pq_remove_first();
			if (i == t)
				return dd[i];
			for (int o = eo[i]; o-- > 0; ) {
				int j = ej[i][o], w = ew[i][o];
				long d = dd[i] + w;
				if (dd[j] > d) {
					if (dd[j] == INF)
						pq_add_last(j);
					dd[j] = d; pq_up(j);
				}
			}
		}
		return -1;
	}

	void main() {
		int x0 = sc.nextInt();
		int y0 = sc.nextInt();
		int x1 = sc.nextInt();
		int y1 = sc.nextInt();
		n = sc.nextInt() + 2;
		vv = new V[n];
		vv[0] = new V(x0, y0, y0);
		vv[1] = new V(x1, y1, y1);
		for (int i = 2; i < n; i++) {
			int x = sc.nextInt();
			int l = sc.nextInt();
			int r = sc.nextInt() + 1;
			vv[i] = new V(x, l, r);
		}
		Arrays.sort(vv, (u, v) -> u.x - v.x);
		yy = new int[n * 2];
		int n_ = n; n = 0;
		for (int i = 0, j; i < n_; i = j) {
			int x = vv[i].x;
			j = i + 1;
			while (j < n_ && vv[j].x == x)
				j++;
			compress(x, i, j);
		}
		pp = new P[k = n * 2 * 7]; k = 0;
		pp[k++] = new P(x0, y0);
		pp[k++] = new P(x1, y1);
		for (int i = 0; i < n; i++) {
			V v = vv[i];
			pp[k++] = new P(v.x, v.l);
			pp[k++] = new P(v.x, v.r - 1);
		}
		int k_ = k;
		for (int h = 0; h < k_; h++) {
			P p = pp[h];
			for (int x = p.x - 1; x <= p.x + 1; x += 2)
				for (int y = p.y - 1; y <= p.y + 1; y++)
					if (find_vv(x, y) != -1)
						pp[k++] = new P(x, y);
		}
		Arrays.sort(pp, 0, k, (p, q) -> p.x != q.x ? p.x - q.x : p.y - q.y);
		k_ = k; k = 1;
		for (int h = 1; h < k_; h++) {
			P p = pp[h], q = pp[h - 1];
			if (p.x != q.x || p.y != q.y)
				pp[k++] = p;
		}
		init(k);
		for (int h = 1; h < k; h++) {
			P p = pp[h], q = pp[h - 1];
			if (p.x == q.x && find_vv(p.x, p.y) == find_vv(q.x, q.y)) {
				int w = p.y - q.y;
				append(h, h - 1, w);
				append(h - 1, h, w);
			}
		}
		for (int h = 0; h < k; h++) {
			P p = pp[h];
			for (int x = p.x - 1, y = p.y - 1; y <= p.y + 1; y++) {
				int h_ = find_pp(x, y);
				if (h_ != -1) {
					append(h, h_, 1);
					append(h_, h, 1);
				}
			}
		}
		println(dijkstra(k, find_pp(x0, y0), find_pp(x1, y1)));
	}
}
