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

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.text.*;

public class cf242c_bfs_dfs {

    static BufferedReader br;
    static Scanner sc;
    static PrintWriter out;

    public static void initA() {
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
            //br = new BufferedReader(new FileReader("input.txt"));
            sc = new Scanner(System.in);
            //out = new PrintWriter("output.txt");
            out = new PrintWriter(System.out);
        } catch (Exception e) {
        }
    }

    public static void initB() {
        try {

            br = new BufferedReader(new FileReader("input.txt"));
            sc = new Scanner(new FileReader("input.txt"));
            out = new PrintWriter("output.txt");

        } catch (Exception e) {
        }
    }

    public static String getString() {
        try {
            return br.readLine();
        } catch (Exception e) {
        }
        return "";
    }

    public static Integer getInt() {
        try {
            return Integer.parseInt(br.readLine());
        } catch (Exception e) {
        }
        return 0;
    }

    public static Integer[] getIntArr() {
        try {
            StringTokenizer temp = new StringTokenizer(br.readLine());
            int n = temp.countTokens();
            Integer temp2[] = new Integer[n];
            for (int i = 0; i < n; i++) {
                temp2[i] = Integer.parseInt(temp.nextToken());
            }
            return temp2;
        } catch (Exception e) {
        }
        return null;
    }

    public static Long[] getLongArr() {
        try {
            StringTokenizer temp = new StringTokenizer(br.readLine());
            int n = temp.countTokens();
            Long temp2[] = new Long[n];
            for (int i = 0; i < n; i++) {
                temp2[i] = Long.parseLong(temp.nextToken());
            }
            return temp2;
        } catch (Exception e) {
        }
        return null;
    }

    public static String[] getStringArr() {
        try {
            StringTokenizer temp = new StringTokenizer(br.readLine());
            int n = temp.countTokens();
            String temp2[] = new String[n];
            for (int i = 0; i < n; i++) {
                temp2[i] = (temp.nextToken());
            }
            return temp2;
        } catch (Exception e) {
        }
        return null;
    }

    public static int getMax(Integer[] ar) {
        int t = ar[0];
        for (int i = 0; i < ar.length; i++) {
            if (ar[i] > t) {
                t = ar[i];
            }
        }
        return t;
    }

    public static void print(Object a) {
        out.println(a);
    }

    public static int nextInt() {
        return sc.nextInt();
    }

    public static double nextDouble() {
        return sc.nextDouble();
    }

    public static void main(String[] ar) {
        initA();
        solve2();
        out.flush();
    }
    static HashMap<Integer, ArrayList<Integer[]>> available = new HashMap<Integer, ArrayList<Integer[]>>();
    static HashMap<Integer, HashSet<Integer>> sdh = new HashMap<Integer, HashSet<Integer>>();

    public static void solve2() {
        Integer sad[] = getIntArr();

        point start = new point(sad[0], sad[1], 0);

        int x_fin = sad[2];
        int y_fin = sad[3];

        if (sad[0] - sad[2] == 0 && sad[1] - sad[3] == 0) {
            print(0);
            return;
        }

        int dsa = getInt();

        for (int i = 0; i < dsa; i++) {
            sad = getIntArr();

            int row = sad[0];
            Integer s[] = {sad[1], sad[2]};
            if (available.containsKey(row)) {
                available.get(row).add(s);
            } else {
                ArrayList<Integer[]> init = new ArrayList<Integer[]>();
                init.add(s);
                available.put(row, init);
            }
        }
        
        if(!dfs(start, x_fin, y_fin)){
            print(-1);
        }
        
    }

    public static void solve() {
        Integer sad[] = getIntArr();

        point start = new point(sad[0], sad[1], 0);

        int x_fin = sad[2];
        int y_fin = sad[3];

        if (sad[0] - sad[2] == 0 && sad[1] - sad[3] == 0) {
            print(0);
            return;
        }

        int dsa = getInt();



        for (int i = 0; i < dsa; i++) {
            sad = getIntArr();

            int row = sad[0];
            Integer s[] = {sad[1], sad[2]};
            if (available.containsKey(row)) {
                available.get(row).add(s);
            } else {
                ArrayList<Integer[]> init = new ArrayList<Integer[]>();
                init.add(s);
                available.put(row, init);
            }
        }

        //ArrayList<point> jalanku = new ArrayList<point>();
        Queue<point> jalanku = new ArrayDeque<point>();


        jalanku.add(start);
        HashSet<Integer> inis = new HashSet<Integer>();
        inis.add(start.y);
        sdh.put(start.x, inis);
        /*
        for (int is = 0; is < jalanku.size(); is++) {
        ArrayList<point> cabang = jalanku.get(is).getCabang();
        
         * 
         */
        for (; jalanku.size() > 0;) {
            ArrayList<point> cabang = jalanku.remove().getCabang();
            for (point p : cabang) {
                jalanku.add(p);
                //print("AKU JALAN KE " + p.x + "," + p.y + " dgn jarak " + (is + 1));
                if (p.x == x_fin && p.y == y_fin) {
                    print(p.d);
                    return;
                }
            }

        }

        print(-1);


    }

    static boolean dfs(point s, int x, int y) {
        boolean found = false;
        ArrayList<point> cabang = s.getCabang();
        for (point p : cabang) {
            if(p.x==x && p.y==y){
                print(p.d);
                return true;
            }
            found |= dfs(p, x, y);
        }
        return found;
    }

    private static class point {

        int x, y, d;

        point(int a, int b, int d) {
            x = a;
            y = b;
            this.d = d;
        }

        void hihi(int a, int b, ArrayList<point> arl) {
            if (isAvailable(a, b)) {
                if (!sdh.containsKey(a)) {
                    HashSet<Integer> ini = new HashSet<Integer>();
                    ini.add(b);
                    sdh.put(a, ini);
                    arl.add(new point(a, b, d + 1));
                }
                if (sdh.containsKey(a) && !sdh.get(a).contains(b)) {
                    sdh.get(a).add(b);
                    arl.add(new point(a, b, d + 1));
                }

            }
        }

        ArrayList<point> getCabang() {
            ArrayList<point> temp = new ArrayList<point>();

            hihi(x - 1, y - 1, temp);
            hihi(x - 1, y, temp);
            hihi(x - 1, y + 1, temp);
            hihi(x, y - 1, temp);
            hihi(x, y + 1, temp);
            hihi(x + 1, y - 1, temp);
            hihi(x + 1, y, temp);
            hihi(x + 1, y + 1, temp);
            return temp;
        }

        boolean isAvailable(int x, int y) {
            if (available.containsKey(x)) {
                for (Integer[] b : available.get(x)) {
                    if (y >= b[0] && y <= b[1]) {
                        return true;
                    }
                }
                return false;
            }
            return false;
        }
    }
}
