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

/*
the basic idea is to use a matrix to represent the chess board,
but this definitely would use a lot of space
so use a hashamp to store the information, use a Segment class to represent the range
use bfs to find the min stpes 
*/

import java.io.*;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Queue;
import java.util.LinkedList;
public class KingsPath {

	private static class Segment{
		int left;
		int right;
		public Segment(int l, int r) {
			left = l;
			right = r;
		}
	}


    static BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter printWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    static final int DIMENSION = 1000000001;
    public static void main(String[] args) throws IOException{
    	String[] inputString = bufferedReader.readLine().split(" ");
    	int x0 = Integer.parseInt(inputString[0]);
    	int y0 = Integer.parseInt(inputString[1]);
    	int x1 = Integer.parseInt(inputString[2]);
    	int y1 = Integer.parseInt(inputString[3]);

    	int n = Integer.parseInt(bufferedReader.readLine());
    	HashMap<Integer, HashSet<Integer>> map = new HashMap<Integer,HashSet<Integer>>();

    	// use hashamp to represent all allowed cells
    	for(int i = 0; i < n; i++) {
    		inputString = bufferedReader.readLine().split(" ");
    		int r = Integer.parseInt(inputString[0]);
    		int left = Integer.parseInt(inputString[1]);
    		int right = Integer.parseInt(inputString[2]);

    		if(!map.containsKey(r)) {
    			map.put(r, new HashSet<Integer>());
    		}
    		HashSet<Integer> set = map.get(r);
    		for(int t = left ; t <= right; t++)
    			set.add(t);
    	}

  //  	boolean[][] visited = new boolean[1000000002][1000000002];
        HashMap<Integer,HashSet<Integer>> visited = new HashMap<Integer,HashSet<Integer>>();

    	// then use bfs to find the mininum number of moves to get the end point
    	Queue<Integer> rows = new LinkedList<Integer>();
    	Queue<Integer> cols = new LinkedList<Integer>();

    	
        if(!visited.containsKey(x0)) {
            HashSet<Integer> ttt = new HashSet<Integer>();
            ttt.add(y0);
            visited.put(x0, ttt );
        }

    	rows.offer(x0);
    	cols.offer(y0);
    	long result = -1;
    	boolean found = false;
    	while(!rows.isEmpty() && !found) {
    		
    		int size = rows.size();
    			result++;
    		for(int i = 0; i < size; i++) {
    			int r = rows.poll();
    			int c = cols.poll();

    			if(r == x1 && c == y1) {
    				found = true;
    				break;
    			}

    			// there are eight adjacent cells
    			// goes up
    			if(r - 1 >= 1 &&  (!visited.containsKey(r-1) || !visited.get(r-1).contains(c)) && map.containsKey(r-1) && map.get(r-1).contains(c) ) {
    	           if(!visited.containsKey(r-1))
                   visited.put(r-1, new HashSet<Integer>());
                   visited.get(r-1).add(c);
    				rows.offer(r-1);
    				cols.offer(c);
    			}

    			// goes upper right
    			if(r - 1 >= 0  && c + 1 <= DIMENSION &&  (!visited.containsKey(r-1) || !visited.get(r-1).contains(c+1)) && map.containsKey(r-1) && map.get(r-1).contains(c+1)) {
    	           if(!visited.containsKey(r-1))
                     visited.put(r-1, new HashSet<Integer>());
                   visited.get(r-1).add(c+1);
    				rows.offer(r-1);
    				cols.offer(c+1);
    			}

    			//goes to right
    			if(c + 1 <= DIMENSION &&  (!visited.containsKey(r) || !visited.get(r).contains(c+1)) && map.containsKey(r) && map.get(r).contains(c+1)) {
    	           if(!visited.containsKey(r))
                     visited.put(r, new HashSet<Integer>());
                   visited.get(r).add(c+1);
    				rows.offer(r);
    				cols.offer(c+1);
    			}

    			//goes lower right
    			if(r + 1 <= DIMENSION  && c + 1 <= DIMENSION && (!visited.containsKey(r+1) || visited.get(r+1).contains(c+1)) && map.containsKey(r+1) && map.get(r+1).contains(c+1)) {
    	           if(!visited.containsKey(r+1))
                     visited.put(r+1, new HashSet<Integer>());
                   visited.get(r+1).add(c+1);
    				rows.offer(r+1);
    				cols.offer(c+1);
    			}

    			//goes down
    			if(r + 1 <= DIMENSION &&  (!visited.containsKey(r+1) || !visited.get(r+1).contains(c)) && map.containsKey(r+1) && map.get(r+1).contains(c) ) {
    	           if(!visited.containsKey(r+1))
                     visited.put(r+1, new HashSet<Integer>());
                   visited.get(r+1).add(c);
    				rows.offer(r+1);
    				cols.offer(c);
    			}

    			// goes lower left
    			if(r+1 <= DIMENSION  && c - 1 >= 0 &&  (!visited.containsKey(r+1) || !visited.get(r+1).contains(c-1)) && map.containsKey(r+1) && map.get(r+1).contains(c-1)) {
    			           if(!visited.containsKey(r+1))
                             visited.put(r+1, new HashSet<Integer>());
                           visited.get(r+1).add(c-1);
    				rows.offer(r+1);
    				cols.offer(c-1);
    			}

    			// goes left
    			if(c - 1 >= 0 &&  (!visited.containsKey(r) || !visited.get(r).contains(c-1)) && map.containsKey(r) && map.get(r).contains(c-1)) {
    	           if(!visited.containsKey(r))
                     visited.put(r, new HashSet<Integer>());
                   visited.get(r).add(c-1);
    				rows.offer(r);
    				cols.offer(c-1);
    			}

    			if(c - 1 >= 0 && r - 1 >= 0 &&  (!visited.containsKey(r-1) || !visited.get(r-1).contains(c-1)) && map.containsKey(r-1) && map.get(r-1).contains(c-1)) {
    			           if(!visited.containsKey(r-1))
                             visited.put(r-1, new HashSet<Integer>());
                           visited.get(r-1).add(c-1);
    				rows.offer(r-1);
    				cols.offer(c-1);
    			}
    		}
    	}

    	if(!found) {
    		printWriter.println(-1);
    	} else {
    		printWriter.println(result);
    	}

    	printWriter.flush();
    	printWriter.close();



    }
}





























