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

/**
* Created with IntelliJ IDEA.
* User: shenchen
* Date: 12-11-12
* Time: AM7:36
* To change this template use File | Settings | File Templates.
*/
import java.io.*;
import java.util.*;

public class KingsPath implements Runnable {

    BufferedReader in;
    PrintWriter out;
    StringTokenizer tok = new StringTokenizer("");

    public static void main(String[] args) {
        new KingsPath().run();
    }

    public void run() {
        try {
            long t1 = System.currentTimeMillis();
            if (System.getProperty("ONLINE_JUDGE") != null) {
                in = new BufferedReader(new InputStreamReader(System.in));
                out = new PrintWriter(System.out);
            } else {
                in = new BufferedReader(new FileReader("/Users/shenchen/input.txt"));
                out = new PrintWriter("/Users/shenchen/output.txt");
            }
            Locale.setDefault(Locale.US);
            solve();
            in.close();
            out.close();
            long t2 = System.currentTimeMillis();
            System.err.println("Time = " + (t2 - t1));
        } catch (Throwable t) {
            t.printStackTrace(System.err);
            System.exit(-1);
        }
    }

    String readString() throws IOException {
        while (!tok.hasMoreTokens()) {
            tok = new StringTokenizer(in.readLine());
        }
        return tok.nextToken();
    }

    int readInt() throws IOException {
        return Integer.parseInt(readString());
    }

    long readLong() throws IOException {
        return Long.parseLong(readString());
    }

    double readDouble() throws IOException {
        return Double.parseDouble(readString());
    }

    Set<pair> avail;
    Set<pair> visit;

    void solve() throws IOException {
        int x0, y0, x1, y1;
        x0 = readInt(); y0 = readInt(); x1 = readInt(); y1 = readInt();
        avail = new HashSet<pair>();
        visit = new HashSet<pair>();

        int n = readInt();
        for (int i = 0; i < n; i ++) {
            int r = readInt();
            int a = readInt();
            int b = readInt();
            for (int j = a; j <=b; j ++)
                avail.add(new pair(r, j));
        }

        Queue<Integer> q = new LinkedList<Integer>();
        visit.add(new pair(x0, y0));
        q.add(x0);
        q.add(y0);
        q.add(0);
        int ret = -1;
        while (q.size() > 0) {
            int x = q.poll(); int y = q.poll(); int step = q.poll();
            if (x == x1 && y == y1)  {
                ret = step;
                break;
            }
            for (int j = -1; j <= 1; j ++)
            for (int i = -1; i <= 1; i ++) {
                if (i == 0 && j == 0)
                    continue;
                int nx = x + i;
                int ny = y + j;
                pair p = new pair(nx, ny);
                if (avail.contains(p) && !visit.contains(p)) {
                    visit.add(p);
                    q.add(nx);
                    q.add(ny);
                    q.add(step + 1);
                }
            }
        }
        out.println(ret);
    };

    class pair {
        int x;
        int y;

        pair(int x, int y) {
            this.x = x;
            this.y = y;
        }

        public boolean equals(Object o) {
            if (o == null || !(o instanceof pair))
                return false;
            pair p1 = (pair)o;
            return p1.x == x && p1.y == y;
        }

        public int hashCode() {
            return x * 133 + y;
        }
    }

}