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.awt.Point;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;


public class C {
    
    static StringTokenizer st;
    static BufferedReader in;
    static class Sort implements Comparable<Sort> {
        int L, R;
        public int compareTo(Sort o) {
            if (this.L==o.L)
                return this.R-o.R;
            return this.L-o.L;
        }
        public Sort(int L, int R) {
            this.L = L;
            this.R = R;
        }
    }
    public static void main(String[] args) throws IOException {
        in = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
        int y1 = nextInt();
        int x1 = nextInt();
        int y2 = nextInt();
        int x2 = nextInt();
        int n = nextInt();
        ArrayList<Sort>[] L = new ArrayList[n+1];
        for (int i = 1; i <= n; i++) {
            L[i] = new ArrayList<Sort>();
        }
        Map<Integer, Integer> map = new TreeMap<Integer, Integer>();
        int[]row = new int[n+1];
        int cnt = 0;
        for (int i = 1; i <= n; i++) {
            int r = nextInt();
            int a = nextInt();
            int b = nextInt();
            if (!map.containsKey(r)) {
                map.put(r, ++cnt);
                row[cnt] = r;
            }
            L[map.get(r)].add(new Sort(a, b));
        }
        for (int i = 1; i <= cnt; i++) {
            Collections.sort(L[i]);
        }
        Set<Point> set = new HashSet<Point>();
        for (int i = 1; i <= cnt; i++) {
            int max = 1;
            for (Sort p : L[i]) {
                for (int j = Math.max(p.L, max); j <= p.R; j++) {
                    set.add(new Point(j, row[i]));
                }
                max = Math.max(max, p.R);
            }
        }
        Queue<Point> q = new LinkedList<Point>();
        Queue<Integer> xod = new LinkedList<Integer>();
        q.add(new Point(x1, y1));
        int[]dx = {1, 1, -1, -1, 1, 0, -1, 0};
        int[]dy = {-1, 1, 1, -1, 0, 1, 0, -1};
        xod.add(0);
        Set<Point> used = new HashSet<Point>();
        used.add(new Point(x1, y1));
        while (!q.isEmpty()) {
            Point p = q.poll();
            int num = xod.poll();
            if (p.x==x2 && p.y==y2) {
                System.out.println(num);
                return;
            }
            for (int i = 0; i < 8; i++) {
                int x = p.x+dx[i], y = p.y+dy[i];
                Point pp = new Point(x, y);
                if (x >= 1 && x <= 1e9 && y >= 1 && y <= 1e9 && !used.contains(pp) && set.contains(pp)) {
                    used.add(pp);
                    q.add(pp);
                    xod.add(num+1);
                }
            }
        }
        System.out.println(-1);
        pw.close();
    }
    private static int nextInt() throws IOException{
        return Integer.parseInt(next());
    }
    
    private static long nextLong() throws IOException{
        return Long.parseLong(next());
    }
    
    private static double nextDouble() throws IOException{
        return Double.parseDouble(next());
    }
    
    private static String next() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(in.readLine());
        }
        return st.nextToken();
    }
}
