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.util.Map;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.util.HashMap;
import java.util.Set;
import java.io.PrintStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.AbstractCollection;
import java.util.LinkedList;
import java.util.HashSet;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author Siddharth
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		OutputWriter out = new OutputWriter(outputStream);
		TaskC solver = new TaskC();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskC {

    public boolean preProcess(int row, int column, int distance, HashSet<Pair> marked, HashMap<Integer, Pair> accessibleCells) {
        if (accessibleCells.containsKey(row)) {
             Pair redeem = accessibleCells.get(row);
             if (column >= redeem.element0 && column <= redeem.element1) {
                 if (!marked.contains(new Pair(row, column))) {
                      return true;
                 }
             }
        }
        return false;
    }

	public void solve(int testNumber, InputReader in, OutputWriter out) {
        int s_x = in.nextInt();
        int s_y = in.nextInt();
        int f_x = in.nextInt();
        int f_y = in.nextInt();
        int n = in.nextInt();
        HashMap<Integer, Pair> accessibleCells = new HashMap<Integer, Pair>();
        for (int i = 0; i < n; i++) {
            int row = in.nextInt();
            Pair coord = new Pair(in.nextInt(), in.nextInt());
            if (accessibleCells.containsKey(row)) {
                Pair redeem = accessibleCells.get(row);
                if (redeem.getElement0() < coord.getElement0())
                    coord.element0 = redeem.getElement0();
                if (redeem.element1 > coord.element1)
                    coord.element1 = redeem.element1;
                accessibleCells.put(row, coord);
            } else {
                accessibleCells.put(row, coord);
            }
        }
        for (Map.Entry<Integer, Pair> entry: accessibleCells.entrySet()) {
            System.out.println(entry.getKey() + " / " + entry.getValue());
        }
        // BFS
        LinkedList<Integer[]> nodes = new LinkedList<Integer[]>();
        Pair end = new Pair(f_x, f_y);
        HashSet<Pair> marked = new HashSet<Pair>();
        Integer []start = new Integer[3];
        start[0] = s_x;
        start[1] = s_y;
        start[2] = 0;
        marked.add(new Pair(start[0], start[1]));
        nodes.add(start);
        while (!nodes.isEmpty()) {
            for (Integer[] entry : nodes) {
                System.out.print("(" + entry[0] + "," + entry[1] + ") ");
            }
            Integer []node = nodes.removeFirst();
            System.out.println("Accessing ("+node[0]+", "+node[1]+") -");
            for (int i = -1; i <= 1; i++) {
                for (int j = -1; j <= 1; j++) {
                    boolean toAdd = preProcess(node[0]+i, node[1]+j, node[2]+1, marked, accessibleCells);
                    if (toAdd) {
                        Integer []nodeToAdd = new Integer[3];
                        nodeToAdd[0] = node[0]+i;
                        nodeToAdd[1] = node[1]+j;
                        nodeToAdd[2] = node[2]+1;
                        nodes.add(nodeToAdd);
                        Pair nodePair = new Pair(nodeToAdd[0], nodeToAdd[1]);
                        marked.add(new Pair(nodeToAdd[0], nodeToAdd[1]));
                        if (nodePair.equals(end)) {
                            out.println(nodeToAdd[2]);
                            return;
                        }
                    }
                }
            }
        }
        out.println(-1);
        return;
	}

    class Pair implements Comparable<Integer> {

        Integer element0;
        Integer element1;

        public Pair(Integer element0, Integer element1) {
            this.element0 = element0;
            this.element1 = element1;
        }

        public Integer getElement0() {
            return element0;
        }

        public String toString() {
            return "("+element0+", "+element1+")";
        }
        public int compareTo(Integer t) {
            return 0;  //To change body of implemented methods use File | Settings | File Templates.
        }

        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            Pair pair = (Pair) o;

            if (element0 != null ? !element0.equals(pair.element0) : pair.element0 != null) return false;
            if (element1 != null ? !element1.equals(pair.element1) : pair.element1 != null) return false;

            return true;
        }

        public int hashCode() {
            int result = element0 != null ? element0.hashCode() : 0;
            result = 31 * result + (element1 != null ? element1.hashCode() : 0);
            return result;
        }
    }
}

class InputReader {

    private InputStream stream;
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;
    private SpaceCharFilter filter;

    public InputReader(InputStream stream) {
        this.stream = stream;
    }

    public int read() {
        if (numChars == -1)
            throw new InputMismatchException();
        if (curChar >= numChars) {
            curChar = 0;
            try {
                numChars = stream.read(buf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (numChars <= 0)
                return -1;
        }
        return buf[curChar++];
    }

    public int nextInt() {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        int sgn = 1;
        if (c == '-') {
            sgn = -1;
            c = read();
        }
        int res = 0;
        do {
            if (c < '0' || c > '9')
                throw new InputMismatchException();
            res *= 10;
            res += c - '0';
            c = read();
        } while (!isSpaceChar(c));
        return res * sgn;
    }

    public boolean isSpaceChar(int c) {
        if (filter != null)
            return filter.isSpaceChar(c);
        return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    }

    public interface SpaceCharFilter {
        public boolean isSpaceChar(int ch);
    }
}

class OutputWriter {
    private final PrintWriter writer;

    public OutputWriter(OutputStream outputStream) {
        writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
    }

    public OutputWriter(Writer writer) {
        this.writer = new PrintWriter(writer);
    }

    public void print(Object...objects) {
        for (int i = 0; i < objects.length; i++) {
            if (i != 0)
                writer.print(' ');
            writer.print(objects[i]);
        }
    }

    public void println(Object...objects) {
        print(objects);
        writer.println();
    }

    public void close() {
        writer.close();
    }

}

